0
点赞
收藏
分享

微信扫一扫

unity

龙毓七七 2022-02-18 阅读 40

Inspector窗口可编辑的变量

1.让私有属性在inspector面板上修改

[SerializeField]
private int age;

2.公共变量不让其显示编辑

[HideInInspector]
public string name1;

3.大部分类型都能被在inspector窗口中被显示编辑

不支持字典和自定义类型

支持数组、列表、枚举、unity内置类

public enum E_enum
{
    Normal,
    Player,
    Monster
}

public struct MyStruct
{
    public int age;
    public string name;
}

public class MyClass
{
    public int age;
    public string name;
}

public class test : MonoBehaviour
{
    public int[] array;
    public List<int> list;
    public E_enum type;
    public GameObject obj;

    public Dictionary<int, string> dic;

    //自定义变量类型
    public MyStruct mystruct;
    public MyClass muclass;

}

让自定义类型可以被访问

[System.Serializable]
public struct MyStruct
{
    public int age;
    public string name;
}

4.辅助特性

[Header("个人信息")]
public string name;
public int age;
public bool sex;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a0SL3tjb-1645121379064)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220216171445259.png)]

[Tooltip("家庭")] //鼠标悬停注释
public int family;

[Range(0, 5)] //设定滑条值
[Space()] //空一行
public int luck;

[Multiline(4)]//显示多少行字符串
public string text;

[ContextMenuItem("重置", "demo")]//为变量添加快捷方法
public int money;
private void demo()
{
	money = 100;
}

[ContextMenu("测试方法")]
private void Test()
{
	print("测试");
}

MonoBehavior

1.打印组件名字

print(this.gameObject.name);

2.得到组件位置信息

print(this.transform.position);
print(this.transform.eulerAngles);
print(this.transform.lossyScale);
//跟this.gameObject.transform一样

3.失活脚本

this.enabled = false;

得到依附对象(组件)上挂载的其他脚本(重要)

1.得到自己挂载的单个脚本

test1 t = this.GetComponent<test1>();

2.得到自己挂载的多个脚本,得到的是一个数组,不常用

FindOtherScript[] array = this.GetComponents<FindOtherScript>();

3.得到子对象挂载的脚本,可以设定布尔值

test t = this.GetComponentInChildren<test>(true);

GameObject

1.成员变量

print(this.gameObject.name);        
print(this.gameObject.activeSelf);
print(this.gameObject.isStatic);
print(this.gameObject.tag);
print(this.gameObject.layer);
print(this.gameObject.transform.position);

2.静态方法

//找单个对象,找不到失活对象,无法确定场上多个相同对象
GameObject obj = GameObject.Find("名字");
Gameobject obj = Gameobject.FindGameObjectWithTag("Player");
	//获取单个对象还可用public

//查找多个对象,只能通过tag找
GameObject[] obj = GameObject.FindGameObjectsWithTag("Player");

//C#中System->Object->GameObject

//找到场景中挂载的某一个脚本对象,找到挂载在别的组件上的脚本从而获取组件信息
//效率很低
test t = GameObject.FindObjectOfType<test>();

//实例化对象(克隆对象),往往关联预设体
public GameObject obj;
void Start()
{
    GameObject.Instantiate(obj);
    
    //还可用以下方法,因为此方法是Object基类提供的
    Instantiate(obj);
    Object.Instantiate(obj);
}

//删除对象,第二个参数延迟几秒后删除,也可以删除脚本
//不会马上移除对象,会在下一帧把对象移除并从内存删除
//也为Object基类提供
GameObject.Destory(obj,5);
//立即将对象从内存中移除
GameObject.DestroyImmediate(obj);

//如果是继承MonoBehavior的类,不用写GameObject
Destory();

//过场景不移除
//默认情况 在切换场景时 场景中对象会被自动删除
//一般传自己依附的GameObject对象
GameObject.DontDestroyOnLoad(this.gameObject);
//如果继承MonoBehavoir
DontDestroyOnLoad(this.gameObject);

寻找对象效率都比较低,都是早期用,早期也一般先用public

3.成员方法

//1.创建一个空物体
GameObject obj = new GameObject();
GameObject obj2 = new GameObject("Atuoa");
//挂载test,test1脚本
GameObject obj3 = new GameObject("Atuoa", typeof(test), typeof(test1));

//2.为对象动态添加脚本(重要)
obj.AddComponent<test>();

//3.标签比较
if(this.gameObject.tag == "Player")
{
    print("yes");
}

if (this.gameObject.CompareTag("Player"))
{
    print("yes");
}

//失活
obj.SetActive(false);
举报

相关推荐

unity笔记

Unity——MVC

Unity 向量

Unity Navigation

unity Shader

Unity PlayerPrefs

0 条评论