using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Declaration : MonoBehaviour
{
public GameObject ObjectPublic;//声明访问权限为公有的GameObject(引用)类型变量 ObjectPublic
protected GameObject ObjectProtected;//声明访问权限为受保护的GameObject(引用)类型变量 ObjectPublic
private GameObject ObjectPrivate;//声明访问权限为私有的GameObject(引用)类型变量 ObjectPublic
GameObject objectFriendly;//声明普通的未定义访问权限的变量
[SerializeField]//在inspector面板中显示。强制unity去序列化一个私有域。这是一个内部的unity序列化功能,有时候我们需要Serialize一个private或者protected的属性,
//这个时候可以使用[SerializeField]这个Attribute:之后就可以在面板看到该变量。
GameObject objectSerialized;
[HideInInspector]//在inspector中隐藏。下面这个已经序列化的变量,加上这个HideInInspector后,将不显示在面板
public GameObject objectNotPublic;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Variables : MonoBehaviour
{
// Start is called before the first frame update
public bool booleanValue;//声明布尔型变量
public float floatValue;//声明浮点数变量
public int intValue;//声明整型变量
public Vector2 vector2Value;//声明二维向量
public Vector3 vector3Value;//声明三维向量变量
public Quaternion quaternionValue;//声明四元数变量
public byte byteValue;
public string stringValue;
public enum emType{A,B,C}//自定义一个枚举类型,名为emType
public emType em;//实例化
public GameObject otherObject;//声明GameObject引用类型的变量
public test testScript;//声明test引用类型的变量,是一个特定的引用类型
[System.Serializable]
public class SpaceShipComponent{
public enum SpaceShipComponentType{
Weapon,
Storage,
Shield,
Command,
Power,
Structural
}
public SpaceShipComponentType type;
public int hitPoints=0;
public int armor=0;
}
public SpaceShipComponent spaceShipComponent;
void Start()
{
}
}
test脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// Start is called before the first frame update
GameObject obj;
void Start()
{
}
void Awake(){
obj=GameObject.Find("Cube");
}
public void getObj(GameObject go){
if(obj==null){
Debug.LogError("Null Reference!!!!");
return;
}
go.transform.parent=obj.transform;
}
// Update is called once per frame
void Update()
{
}
}