一:(碰撞体)触发信息检测:
发生碰撞的两个物体应满足的条件:
NO1:两物体都应该有碰撞体
NO2:其中一个带刚体;
NO3:检测触发信息的脚本需绑定在带刚体的物体上
1.MonoBehaviour.OnCollisionEnter( Collision collisionInfo ) 当进入碰撞器
2.MonoBehaviour.OnCollisionExit( Collision collisionInfo ) 当退出碰撞器
3.MonoBehaviour.OnCollisionStay( Collision collisionInfo ) 当逗留碰撞器
获取材质颜色
Public Cube1;//脚本绑在Cube1上,碰撞器
Public Cube2;//Cube2进行碰撞移动的物体
Public render rend;
void Start ()
{
//获取当前物体的组件
rend = GetComponent<Renderer>();
}
void OnCollisionEnter(Collision collisionInfo)
{
//Debug.Log(gameobject.name);
//Cube2获取材质黄色 collisionInfo.gameObject.GetComponent<Renderer>().material.color = Color.yellow;
Cube2.transform.Translate(Vector3.down *2f * Time.deltaTime);
}
void OnCollisionStay( Collision collisionInfo )
{
Debug.Log(collisionInfo.gameObject);
//cube.position = Vector3.MoveTowards(new Vector3(3,2,0),new Vector3(3,5,0),2f);
//cube.transform.Translate(Vector3.forward * 2f);
cube.transform.Translate(Vector3.right*0.5f*Time.deltaTime);
rend.material.color = Color.blue;
//Cube1获取颜色为蓝色
}
void OnCollisionExit(Collision collisionInfo)
{
Debug.Log("碰撞结束!");
//Cube1获取颜色为黑色
rend.material.color = Color.black;
}
二:(触发器)触发信息检测:
发生碰撞的两个物体应满足的条件:
NO1:两物体都应该有碰撞体;
NO2:最少要有一个刚体碰撞器并且勾选了IsTrigger复选框,另一个最少要有一个碰撞器组件;
NO3:检测碰撞的脚本必须附加在那个带有刚体的触发器;
1.MonoBehaviour.OnTriggerEnter( Collider other )当进入触发器
2.MonoBehaviour.OnTriggerExit( Collider other )当退出触发器
3.MonoBehaviour.OnTriggerStay( Collider other )当逗留触发器
三:加载场景
using UnityEngine.SceneManagement;
SceneManager.LoadScene("Simulation training Scene");
四:鼠标跟随(坐标转换)
Canvas canvas;
RectTransform rectTransform;
Vector2 pos;
Vector3 result;
void Start()
{
rectTransform = transform as RectTransform;
canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
}
void Update()
{
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out pos))
{
rectTransform.anchoredPosition = pos;
rectTransform.parent = canvas.transform;
}
}
五:Toggle添加监听
public GameObject Tog3;
Tog3.GetComponent<Toggle>().onValueChanged.AddListener(delegate
{
this.OnClick(Tog3);
});
void OnClick( GameObject tog)
{
}
六:文字标签 显示:
public List<GameObject> ToolPic=new List<GameObject>();
public List<GameObject> ToolPicName = new List<GameObject>();
void Start()
{
for (int i = 0; i < ToolPic.Count; i++)
{
EventTriggerListener.Get(ToolPic[i]).onEnter += OnMouseEnterImage;
EventTriggerListener.Get(ToolPic[i]).onExit += OnMouseExitImage;
}
}
void OnMouseEnterImage(GameObject btn)
{
for (int i = 0; i < ToolPic.Count; i++)
{ //根据图片显示对应的文本
if (btn == ToolPic[i])
{
ToolPicName[i].SetActive(true);
}
}
}
void OnMouseExitImage(GameObject btn)
{
for (int i = 0; i < ToolPic.Count; i++)
{
if (btn == ToolPic[i])
{
ToolPicName[i].SetActive(false);
}
}
}
七:3D 物体360度旋转
public Transform target;
public float speed = 1f;
void Start()
{
if (target == null) target = transform;
}
public void OnDrag(PointerEventData eventData)
{
//方法一:
//target.localEulerAngles = new Vector3(0f, target.localEulerAngles.y + eventData.position.y - y, 0f);
//y = eventData.position.y;
//方法二:
target.localRotation = Quaternion.Euler(0f, -0.5f * eventData.delta.x * speed, 0f) * target.localRotation;
}
public void OnBeginDrag(PointerEventData eventData)
{
y = eventData.position.y;
}