使用enum property BOM2 项目中需要有一个单独的script控制mode的切换,其他scipt根据这个mode切换到哪一个enum来决定该mode下的一些功能的开启。一开 始我用的是inspector的方法,在enum的script里控制enum选择的值,然后在需要做判断的script里创建一个pub
使用enum property
BOM2 项目中需要有一个单独的script控制mode的切换,其他scipt根据这个mode切换到哪一个enum来决定该mode下的一些功能的开启。一开 始我用的是inspector的方法,在enum的script里控制enum选择的值,然后在需要做判断的script里创建一个public变量来接 受enum值的变化。后来我改为完全用类的enum方法实现。
<span>//</span><span>ModeSelector.cs</span> <span>using</span><span> UnityEngine; </span><span>using</span><span> System.Collections; </span><span>public</span> <span>enum</span><span> ModeEnum { AssemblyView, TearView, ProfileView, TiffView } </span><span>public</span> <span>class</span><span> ModeSelector : MonoBehaviour { </span><span>private</span> <span>static</span> <span>int</span><span> myVar; </span><span>public</span> <span>static</span><span> ModeEnum ModeEnumProperty { </span><span>get</span> { <span>return</span><span> (ModeEnum)myVar; } </span><span>set</span> { myVar = (<span>int</span><span>)value; } } </span><span>//</span><span> Use this for initialization</span> <span>void</span><span> Start() { ModeEnumProperty </span>=<span> ModeEnum.AssemblyView; } </span><span>//</span><span> Update is called once per frame</span> <span>void</span><span> Update() { } </span><span>void</span><span> OnGUI() { </span><span>if</span> (GUI.Button(<span>new</span> Rect(Screen.width / <span>2</span> - <span>150</span>, <span>50</span>, <span>100</span>, <span>20</span>), <span>"</span><span>Tear Apart</span><span>"</span><span>)) { ModeEnumProperty </span>=<span> ModeEnum.TearView; } </span><span>if</span> (GUI.Button(<span>new</span> Rect(Screen.width / <span>2</span> + <span>50</span>, <span>50</span>, <span>100</span>, <span>20</span>), <span>"</span><span>3D Viewer</span><span>"</span><span>)) { ModeEnumProperty </span>=<span> ModeEnum.AssemblyView; } } }</span>
<span>//</span><span>NavigationControl.cs</span> <span> OnGUI() { ... </span><span>if</span>(ModeSelector.ModeEnumProperty ==<span> ModeEnum.AssemblyView) { </span><span>//</span><span>something to do</span> <span> } ... }</span>