环境:Unity3D 4.5.1f3版本
1、创建项目,在场景中放入地层和模型文件。
3、在myGui类中,void OnGUI()函数中添加代码
{ GUI.Label(new Rect(10, 10, 70, 30), "环境光强度"); mAmbientLightValue = GUI.HorizontalSlider(new Rect(80, 15, 100, 30), mAmbientLightValue, 0.0f, 1.0f); int index = (int)(mAmbientLightValue * 255); GUI.Label(new Rect(190, 10, 40, 30), index.ToString()); }
4、 在上面代码后面更随添加改变环境光的代码:
RenderSettings.ambientLight = new Color(mAmbientLightValue, mAmbientLightValue, mAmbientLightValue, 0);
代码如下:
using UnityEngine; using System.Collections; public class directlightMove : MonoBehaviour { private GameObject mPointLight; private GameObject mPlayer; // Use this for initialization void Start () { mPointLight = GameObject.Find("directlight"); mPlayer = GameObject.Find ("construction_worker"); } // Update is called once per frame void Update () { mPointLight.transform.RotateAround(mPlayer.transform.position, mPlayer.transform.forward, Time.deltaTime*30); } }
运行一下,可以看到地面上的阴影随着方向光在移动。
6、让第二个slider控制方向光强度。
在myGui类中,void OnGUI()函数中添加代码:
{ GUI.Label(new Rect(10, 40, 70, 30), "方向光强度"); mLightValue = GUI.HorizontalSlider(new Rect(80, 45, 100, 30), mLightValue, 0.0f, 1.0f); int index = (int)(mLightValue * 255); GUI.Label(new Rect(190, 45, 40, 30), index.ToString()); mPointLight.light.intensity = mLightValue; }
最近新接触的C#和Unity3D,有错误请一定要指教啊!