1.简单托动代码,必须要第一个摄像机深度值比较高照射要拖动的物体,使其实时在物体前面,代码如下
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using DG.Tweening;
public class CarDrag : MonoBehaviour , IBeginDragHandler, IDragHandler, IEndDragHandler ,IPointerDownHandler ,IPointerUpHandler
{
private float offset;
private Vector3 m_distance;
private Vector3 m_origin;
private UnityAction m_action;
private Vector3 m_orignScale;
private bool m_isCanClick;
public GameObject m_effect;
public void OnInitial(UnityAction m_action)
{
this.m_action = m_action;
}
private void Awake()
{
m_isCanClick = true;
m_orignScale = transform.localScale;
}
public void OnPointerDown(PointerEventData eventData)
{
transform.DOScale(m_orignScale * 1.2f,0.2f);
}
public void OnPointerUp(PointerEventData eventData)
{
transform.DOScale(m_orignScale, 0.2F);
}
public void OnBeginDrag(PointerEventData eventData)
{
if (!m_isCanClick)
return;
transform.localScale = m_orignScale * 1.2f;
transform.SetChildrenLayer(transform,LayerMask.NameToLayer("DefaultAdd"));
offset = Camera.main.WorldToScreenPoint(transform.position).z;
m_distance = -Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, offset)) + transform.position;
}
public void OnDrag(PointerEventData eventData)
{
if (!m_isCanClick)
return;
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, offset)) + m_distance;
}
public void OnEndDrag(PointerEventData eventData)
{
if (!m_isCanClick)
return;
List<RaycastResult> m_rayArray = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, m_rayArray);
//var o = m_rayArray.Find(x => x.gameObject.GetComponent<MeshRenderer>().enabled == false);
foreach(var s in m_rayArray)
{
//Debug.LogError(s.gameObject.name + " "+ s.gameObject.tag);
}
var o = m_rayArray.Find(x => x.gameObject.tag == "road");
if (o.gameObject != null)
{
if (GetComponent<NumTags>().m_tagNum == o.gameObject.GetComponent<NumTags>().m_tagNum)
{
transform.DOMove(o.gameObject.transform.position, 0.4F).OnComplete(
() => {
m_effect.SetActive(true); m_action.Invoke();
GetComponent<Animator>().enabled = true;
transform.SetChildrenLayer(transform, LayerMask.NameToLayer("Default"));
});
GetComponent<SphereCollider>().enabled = false;
}
else
{
m_isCanClick = false;
transform.DOLocalMove(Vector3.zero, 0.8f).OnComplete(() => { transform.SetChildrenLayer(transform, LayerMask.NameToLayer("Default"));
m_isCanClick = true;
});
}
}
else
{
m_isCanClick = false;
transform.DOLocalMove(Vector3.zero, 0.8f).OnComplete(() =>
{
transform.SetChildrenLayer(transform, LayerMask.NameToLayer("Default"));
m_isCanClick = true;
});
}
transform.DOScale(m_orignScale, 0.4f);
}
}
2.设置层为XX层里面的扩展类:
using UnityEngine;
using System.Collections.Generic;
using System;
public static class TransformExtension
{
public static int GetActiveChildCount(this Transform transform)
{
int count = 0;
foreach (Transform child in transform)
{
if (child.gameObject.activeSelf)
count++;
}
return count;
}
public static T[] GetComponentsInFirstHierarchyChildren<T>(this Transform transform, bool includeInactive = true) where T : Component
{
List<T> components = new List<T>();
foreach (Transform child in transform)
{
if (!includeInactive && !child.gameObject.activeSelf)
continue;
T component = child.GetComponent<T>();
if (component != null)
components.Add(component);
}
return components.ToArray();
}
public static void SetX(this Transform transform, float x)
{
Vector3 newPosition = new Vector3(x, transform.position.y, transform.position.z);
transform.position = newPosition;
}
public static void SetY(this Transform transform, float y)
{
Vector3 newPosition = new Vector3(transform.position.x, y, transform.position.z);
transform.position = newPosition;
}
public static void SetZ(this Transform transform, float z)
{
Vector3 newPosition = new Vector3(transform.position.x, transform.position.y, z);
transform.position = newPosition;
}
public static void SetXY(this Transform transform, Vector3 position)
{
transform.position = new Vector3(position.x, position.y, transform.position.z);
}
public static void SetXZ(this Transform transform, Vector3 position)
{
transform.position = new Vector3(position.x, transform.position.y, position.z);
}
public static void SetLocalX(this Transform transform, float x)
{
Vector3 newPosition = new Vector3(x, transform.localPosition.y, transform.localPosition.z);
transform.localPosition = newPosition;
}
public static void SetLocalY(this Transform transform, float y)
{
Vector3 newPosition = new Vector3(transform.localPosition.x, y, transform.localPosition.z);
transform.localPosition = newPosition;
}
public static void SetLocalZ(this Transform transform, float z)
{
Vector3 newPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, z);
transform.localPosition = newPosition;
}
public static void SetLocalAngleX(this Transform transform, float x)
{
transform.localEulerAngles = new Vector3(x, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
public static void SetLocalAngleY(this Transform transform, float y)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, y, transform.localEulerAngles.z);
}
public static void SetLocalAngleZ(this Transform transform, float z)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, z);
}
public static string GetPath(this Transform transform)
{
string path = transform.name;
while (transform.parent != null)
{
transform = transform.parent;
path = transform.name + "/" + path;
}
return path;
}
public static int IndexInParent(this Transform transform)
{
if (transform.parent == null)
return -1;
for (int i = 0; i < transform.parent.childCount; i++)
{
if (transform.parent.GetChild(i) == transform)
return i;
}
return -1;
}
public static void SetActiveInChildren(this Transform transform, bool value)
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(value);
}
}
public static void SetLocalScaleX(this Transform transform, float x)
{
Vector3 newScale = new Vector3(x, transform.localScale.y, transform.localScale.z);
transform.localScale = newScale;
}
public static void SetLocalScaleY(this Transform transform, float y)
{
Vector3 newScale = new Vector3(transform.localScale.x, y, transform.localScale.z);
transform.localScale = newScale;
}
public static void SetLocalScaleZ(this Transform transform, float z)
{
Vector3 newScale = new Vector3(transform.localScale.x, transform.localScale.y, z);
transform.localScale = newScale;
}
public static bool EqualsInHierarchy(this Transform transform, Transform other)
{
if (transform.childCount != other.childCount)
return false;
for (int i = 0; i < transform.childCount; i++)
{
if (transform.GetChild(i).name != other.GetChild(i).name)
return false;
else if (!EqualsInHierarchy(transform.GetChild(i), other.GetChild(i)))
return false;
}
return true;
}
public static void DestroyChildren(this Transform transform)
{
for (int i = 0; i < transform.childCount; i++)
UnityEngine.Object.Destroy(transform.GetChild(i).gameObject);
}
public static T GetComponentInChildrenWithName<T>(this Transform transform, string name) where T : Component
{
var array = transform.GetComponentsInChildren<T>();
return Array.Find(array, (item) =>
{
return item.name == name;
});
}
public static void SetChildrenLayer(this Transform transform, Transform transforms, LayerMask layer)
{
foreach (Transform childTransform in transform)
{
childTransform.gameObject.layer = layer;
childTransform.SetChildrenLayer(childTransform, layer);
}
}
}