03:Unity脚本的变量类型以及声明

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()
    {
        
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值