Unity编辑器拓展【C#脚本程序基础】
2018-01-31 20:45
549 查看
Unity几个核心类
Object它是Unity对象的基类,它有一些常用方法:Instantiate用来实例化Unity对象,通过Instantiate创建的物体都会被Unity管理起来。Destroy/DestroyImmediate销毁对象,Destroy在一般运行时候使用,对象在销毁时不是立刻就回收了,DestroyImmediate一般在写编辑器时使用,资源会立刻回收的。
FindObjectOfType/FindObjectsOfType查找某种类型的对象。
https://docs.unity3d.com/ScriptReference/Object.html
GameObject
它是Unity场景中的实体,可以把GameObject拖到Project视图里面弄成Prefab文件。
https://docs.unity3d.com/ScriptReference/GameObject.html
MonoBehavior
它是功能的具体实现体,脚本继承于MonoBehavior后,每个GameObject可以挂脚本实现不同功能。
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
C#变量和命名空间
值类型和引用类型:http://mp.blog.csdn.net/postedit/51202926namespace:http://mp.blog.csdn.net/postedit/77951390
C#预处理指令
C#预处理指令:http://mp.blog.csdn.net/postedit/77971711Unity的宏
UNITY_EDITOR :判定代码是在编辑器模式下执行还是在发布平台下执行UNITY_ANDROID:判定代码运行环境是在andriod平台
UNITY_IPHONE:判定代码运行环境是在ios平台
UNITY_4_6/UNITY_4_6_4:和Unity版本相关,比如这里4_6就表示Unity4.6的版本
C#委托
C#委托:http://mp.blog.csdn.net/postedit/78037951Lambda表达式
只要有委托参数类型的地方,就可以使用Lambda表达式。定义形参:一个参数只写参数名就足够了,多个参数放到()中,可以添加类型,例如://定义
public class UIEventListener : MonoBehaviour
{
public delegate void VoidDelegate(GameObject go);
public UIEventListener.VoidDelegate onClick;
public static UIEventListener Get(GameObject go);
}
//使用例子
UIEventListener.Get(EquipBtn).onClick = go =>
{
//点击事件处理
};
C#异常处理
异常处理:http://mp.blog.csdn.net/postedit/78002305C#反射
反射:http://mp.blog.csdn.net/postedit/78123815代码演示反射的部分用法:using UnityEngine;
using System.Collections;
using System.Reflection;
public class TestReflection : MonoBehaviour {
void OnGUI()
{
if(GUI.Button(new Rect(50,50,200,50), "Display Script Method info"))
{
Component[] components = gameObject.GetComponents<Component>();
int len = components.Length;
for (int i = 0; i < len; i++)
{
Component com = components[i];
Debug.Log(com.ToString()); //打印出所有组件的名字
if(com is TestReflection)
{
PrintMethods(com);//打印组件里面的所有方法
}
}
}
}
/// <summary>
/// 打印组件里面的所有方法
/// </summary>
/// <param name="com">COM.</param>
void PrintMethods(Component com)
{
System.Type comType = com.GetType();
MethodInfo[] methodInfos = comType.GetMethods();
for (int j = 0; j < methodInfos.Length; j++)
{
MethodInfo minfo = methodInfos[j];
ParameterInfo[] parameters = minfo.GetParameters(); //参数信息
string strParam = "";
for (int k = 0; k < parameters.Length; k++)
{
ParameterInfo param = parameters[k];
strParam += param.ParameterType + " " + param.Name;
if(k < parameters.Length - 1)
strParam += ", ";
}
Debug.Log(string.Format("@{0} {1} ({2})", minfo.ReturnType.ToString(), minfo.Name, strParam));
}
}
}
C#Attribute标签
标签:http://mp.blog.csdn.net/postedit/78123322代码演示Attribute的用法:
首先定义一个自定义标签类:
MyAttribute.csusing UnityEngine;
using System;
[AttributeUsage(AttributeTargets.Class)] //规定标签使用在什么地方,这里让标签只能用到类上面
//定义一个标签类
public class MyAttribute : Attribute
{
public string DisplayName;
public MyAttribute(string displayName)
{
DisplayName = displayName;
}
}
接着在一个类中使用这个自定义标签,使用方法如下:using UnityEngine;
using System.Collections;
[MyAttribute("反射测试类")]
//[My("反射测试类")] 这种写法也可以
public class TestAttribute : MonoBehaviour
{
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
然后我们在测试类中查找并打印出刚才定义的标签信息:using UnityEngine;
using System.Collections;
using System.Reflection;
public class TestReflection : MonoBehaviour {
void OnGUI()
{
if(GUI.Button(new Rect(50,50,200,50), "Display Script Method info"))
{
Component[] components = gameObject.GetComponents<Component>();
int len = components.Length;
for (int i = 0; i < len; i++)
{
Component com = components[i];
if( com is TestAttribute)
{
PrintAttributes(com);
}
}
}
}
/// <summary>
/// 打印组件类里面所有的attributes.
/// </summary>
void PrintAttributes(Component com)
{
System.Type attType = com.GetType();
object[] atts = attType.GetCustomAttributes(true); //获取所有的自定义标签
for (int m = 0; m < atts.Length; m++)
{
object attribute = atts[m];
if(attribute is MyAttribute)
{
Debug.Log((attribute as MyAttribute).DisplayName); //显示标签展示名字
}
}
}
}
输出为:
相关文章推荐
- unity编辑器自身出错及unity脚本基础概念
- Unity 3D开发-C#脚本语言的一些基础用法
- 【Unity】6.1 Unity中的C#脚本基础知识
- unity编辑器扩展篇-视图拓展基础
- unity编辑器拓展七——脚本创建模型
- Unity编辑器拓展之五:修改脚本icon
- unity C#脚本开发基础
- unity编辑器拓展九——删除场景中丢失的脚本
- 【unity编辑器拓展】使用脚本添加Prefab到场景中
- Unity脚本开发基础 C#
- 【Unity基础知识②】创建C#脚本、Debug.log输出语句
- Unity编辑器拓展【Unity GUI基础】
- 编译时指定程序入口—C#基础回顾
- Unity 3d中导入c#脚本时出现 can't add script 如何解决
- unity经典入门课程之C#基础实例讲解(打砖块实例)
- Unity修改C#默认创建Script 脚本格式
- C#编写Unity基础GUI之控件布局
- unity编辑器拓展十二——储存工具栏的数据
- [unity基础教程] unity Mathf 数学运算(C#)
- C#Light(包括unity一切C#环境可用嵌入式脚本)0.10B稳定版发布,功能已定型