射线碰撞【拖拽物体img,点击后在固定位置显示A(工具),点击A显示B(Toggle组成的表),关闭B显示C(工具)】

[添加脚本(Move)]

//该脚本挂在img的父物体上

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

//定义枚举

public enum Tools
{
  空,
  塞尺
}

public class Move : MonoBehaviour
{  //放置img的父物体(窗体,如上图所示)
  public GameObject LittleWin1;

  public GameObject Tog1;

      //给窗体设置一个初始位置和目标位置(实现缓动)

  public GameObject TargetPos;
  public GameObject StartPos;

 //Tools类型(枚举),将变量 TOOLS和temoTools 初始化为"空";
  public Tools TOOLS = Tools.空;
  public Tools tempTools = Tools.空;

  Vector3 v;
  void Awake()
  {
    LittleWin1.SetActive(false);
    LittleWin2.SetActive(false);
  }
  void Start ()
  {
    Tog1.GetComponent<Toggle>().onValueChanged.AddListener(delegate
    {
      this.OnClick(Tog1);
    });
  }
  void OnClick(GameObject tog)
  {   
    LittleWin1.SetActive(Tog1 == tog);

           //点击Toggle显示窗体LittleWin(未实现窗体缓动出现的效果)
    if (Tog1.GetComponent<Toggle>().isOn == true)
    {
      LittleWin1.transform.position = Vector3.MoveTowards(TargetPos.transform.position, StartPos.transform.position, 200000f * Time.deltaTime);
    }
    else if (Tog1.GetComponent<Toggle>().isOn == false)
    {
      LittleWin1.transform.position = Vector3.MoveTowards(TargetPos.transform.position, StartPos.transform.position, 200000f * Time.deltaTime);

    }


  }
}

[添加脚本(DragTools)]

//将脚本挂在img上(即要拖拽出来的工具图片img上)

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
public class DragTools : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
  public bool dragOnSurfaces = true;

     //Move类的move对象
  public Move moves;
  public Tools tools;
  PointerEventData eventData;
  private GameObject m_DraggingIcons;
  private RectTransform m_DraggingPlanes;

  public void OnBeginDrag(PointerEventData eventData)
  {
  var canvas = FindInParents<Canvas>(gameObject);
  if (canvas == null)
  return;

  // We have clicked something that can be dragged.
  // What we want to do is create an icon for this.
  m_DraggingIcons = new GameObject("icon");
  Debug.Log("生成图片");
  moves.TOOLS = tools;
  m_DraggingIcons.transform.SetParent (canvas.transform, false);

  //移动该变换到此局部变换列表的末尾。
  m_DraggingIcons.transform.SetAsLastSibling();

  var image = m_DraggingIcons.AddComponent<Image>();
  // The icon will be under the cursor.
  // We want it to be ignored by the event system.
  var group = m_DraggingIcons.AddComponent<CanvasGroup>();
  group.blocksRaycasts = false;
  m_DraggingIcons.AddComponent<MouseFollow>();

  image.sprite = GetComponent<Image>().sprite;
  image.SetNativeSize();
  m_DraggingIcons.GetComponent<Image>().SetNativeSize();

  if (dragOnSurfaces)
  m_DraggingPlanes = transform as RectTransform;
  else
  m_DraggingPlanes = canvas.transform as RectTransform;

  SetDraggedPosition(eventData);
  }

  public void OnDrag(PointerEventData eventData)
  {
    if (m_DraggingIcons != null)
    SetDraggedPosition(eventData);
  }

  private void SetDraggedPosition(PointerEventData eventData)
  {
    if (dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null)
    m_DraggingPlanes= eventData.pointerEnter.transform as RectTransform;

    var rt = m_DraggingIcons.GetComponent<RectTransform>();
    Vector3 globalMousePos;
    if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlanes, eventData.position, eventData.pressEventCamera, out globalMousePos))
    {
      rt.position = globalMousePos;
      rt.rotation = m_DraggingPlanes.rotation;
    }
  }
  void Update()
  {
    if (Input.GetMouseButtonDown(0) && m_DraggingIcons != null)
    {
      Destroy(m_DraggingIcons);

      m_DraggingIcons = null;
      Debug.Log("结束拖拽");
      moves.TOOLS = moves.tempTools;
    }
  }
  public void OnEndDrag(PointerEventData eventData)
  {
    this.eventData = eventData;
  }

  static public T FindInParents<T>(GameObject go) where T : Component
  {
    if (go == null) return null;
    var comp = go.GetComponent<T>();

    if (comp != null)
    return comp;
    var t = go.transform.parent;
    while (t != null && comp == null)
    {
      comp = t.gameObject.GetComponent<T>();
      t = t.parent;
    }
    return comp;
  }
}

[添加脚本(newLine)]

//该脚本挂在在MainCamera上

using UnityEngine;
using UnityEngine.UI;

public class newLine : MonoBehaviour
{
public Transform explosion;
public Transform Tool;
//用于放置工具的父物体(场景中的整个3D模型)
public Transform DeskTran;
//需点击,显示的目标工具
public Transform targetTran;
//Move 类的move对象
public Move move;
//器械展示平台
public Transform Ruler;
//用于放置尺子的父物体
public Transform TargetRuler;


//鼠标跟随
public Camera MainCamera;
public Canvas canvas;
//RectTransform rectTransform;

public GameObject AddSizeWin;

void Update()
{
  //GameObject game = GameObject.Find("SaiChi(Clone)");//寻找任意物体
  //GameObject game=transform.Find("***");//寻找子物体下
  //获取鼠标点击事件
  if (Input.GetMouseButtonDown(0))
  {
    //定义一条射线,从摄像机发出到点击坐标的射线
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    //如果射线发生碰撞
    //Tool.transform.position = Input.mousePosition;
    //Physics.Raycast(射线,碰撞点,射线距离)
    if (Physics.Raycast(ray, out hit, 100))
    {
      //if (Tool == null && move.TOOLS == Tools.塞尺)
      if ( move.TOOLS == Tools.塞尺)
      {//划出射线,只有在scene视图中才能看到
       //Debug.DrawLine(ray.origin, hit.point);

       #region 方法一:射线检测并进行实例化
       GameObject gameObj = hit.collider.gameObject;
      //实例化物体
      Transform theClonedExplosion;
      //hit.point为碰撞点的坐标;
      theClonedExplosion = Instantiate(explosion) as Transform;
      Tool = theClonedExplosion;
      //设置父物体
      Tool.parent = DeskTran;
      Tool.localEulerAngles = Vector3.zero;
      Tool.localPosition = targetTran.localPosition;
      #endregion

      #region 方法二 :直接通过显隐控制
      //Tool = targetTran;
      //targetTran.gameObject.SetActive(true);
      //Tool.parent = DeskTran;
      #endregion
      }
      //点击塞尺出现AddSizeWin窗体
      if (hit.transform.name.Contains("塞尺装配"))
      {
        AddSizeWin.SetActive(true);
        // Debug.Log(hit.transform.parent.name);
      }


      }
   }
  }
}

原文地址:https://www.cnblogs.com/Cocomo/p/5632679.html