1, 物体旋转
void Update () {
transform.Rotate (new Vector3 (0, 1, 0),400);
//transform.Rotate (new Vector3 (0, 1, 0));
//transform.RotateAround (new Vector3 (0, 0, 0)/*旋转的中心*/, new Vector3 (0, 1, 0), 2/*角度*/);
}
2, main camera 上修改背景颜色

3, 屏幕单击的射线
using UnityEngine;
using System.Collections;
public class Rays : MonoBehaviour {
private RaycastHit objhit;
private Ray _ray;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
_ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(_ray, out objhit, 100)) {
GameObject gm = objhit.collider.gameObject;
// Debug.Log ("_rayhit");
// Debug.Log (gm);
gm.GetComponent<MeshRenderer>().material.color = Color.green;
}
}
}
}
3 运行时单击某个物体,出现按钮,单击按钮物体变为设置的颜色。
using UnityEngine;
using System.Collections;
public class Rays : MonoBehaviour {
private RaycastHit objhit;
private Ray _ray;
public GameObject colorPanle;
// Use this for initialization
void Start () {
colorPanle.SetActive (false);//画布是否激活可见
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
_ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//射线从相机发出,经过屏幕上鼠标点击的位置
Debug.DrawLine (_ray.origin, objhit.point, Color.blue, 2);
//画出这条射线
if (Physics.Raycast(_ray, out objhit, 100)) {
//从射线出发最远100m的范围内
// GameObject gm = objhit.collider.gameObject;
// Debug.Log ("_rayhit");
// Debug.Log (gm);
// gm.GetComponent<MeshRenderer>().material.color = Color.green;
colorPanle.SetActive (true);
}
}
}
public void GButtonDown()
{
GameObject obj = GameObject.Find("Sphere");
obj.GetComponent<MeshRenderer> ().material.color = Color.green;
colorPanle.SetActive (false);
}
}
4,找到对象,然后获取下面的脚本,调用脚本里面的函数。
gaze = GameObject.Find ("Gaze").GetComponent<GameGaze>();
gaze.GetPos (light.gameObject, ref x, ref y);
GetPos函数在Gaze游戏对象下面的脚本 GameGaze里面。

内容来源于网络如有侵权请私信删除