本文主要是介绍Unity3D GUI C#脚本实例1——slider 控制环境光,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境: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());}
就会显示上面的那组滑块组,拖动slider滑块的数值显示在后面的Label中,范围是0,255。
4、 在上面代码后面更随添加改变环境光的代码:
RenderSettings.ambientLight = new Color(mAmbientLightValue,mAmbientLightValue, mAmbientLightValue, 0);
编译后运行,在Unity3D中,可以看到随着slider滑动,环境光强弱改变了。
代码如下:
using UnityEngine;
using System.Collections;public class directlightMove : MonoBehaviour {private GameObject mPointLight;private GameObject mPlayer;// Use this for initializationvoid Start () {mPointLight = GameObject.Find("directlight");mPlayer = GameObject.Find ("construction_worker");}// Update is called once per framevoid 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;}
运行一下,方向光在运动,两个slider可以控制两种光。完成!
最近新接触的C#和Unity3D,有错误请一定要指教啊!
这篇关于Unity3D GUI C#脚本实例1——slider 控制环境光的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!