Unity3d创建物体,寻找物体,加载物体,添加脚本

GetCreateObject:

using UnityEngine;

public class GetCreateObject : MonoBehaviour {
    GameObject emptyGo;
    Light goLight;
    GameObject goCamera;
    public Camera pCamera;
    public Transform goRoot;
    Transform transLight;

    GameObject tank;
    
    void Start () {
        //创建物体:在当前场景中创建一个GameObject
        emptyGo = new GameObject("New");

        //寻找物体:获取当前GameObject的Component
        goLight = GetComponent<Light>();
        goLight.color = Color.red;

        //寻找物体:获取当前场景中其他GameObject
        goCamera = GameObject.Find("Main Camera");
        goCamera.transform.Translate(0, 1, -9);

        //创建物体:通过public属性,在Unity中拖动控件的方式
        pCamera.transform.Translate(0, 1, 12);

        //寻找物体:通过工具方法找到物体
        FindChild(goRoot, "Light", ref transLight);
        transLight.GetComponent<Light>().color = Color.green;

        Debug.Log("Test");

        //添加脚本:用代码方式创建GameObject并添加脚本
        tank = new GameObject("Tank");
        tank.AddComponent<Tank>();
    }

    /// <summary>
    /// 寻找物体
    /// </summary>
    /// <param name="trans">作为父物体的tranform</param>
    /// <param name="findName">名称</param>
    /// <param name="_trans">找到的物体</param>
    void FindChild(Transform trans, string findName, ref Transform _trans)
    {
        if (trans.name.Equals(findName))
        {
            _trans = trans.transform;
            return;
        }
        if (trans.childCount != 0)
        {
            for(int i = 0, length = trans.childCount; i < length; i++)
            {
                FindChild(trans.GetChild(i), findName, ref _trans);
            }
        }
    }
}

Tank:

using UnityEngine;

public class Tank : MonoBehaviour {

    //加载物体:拖动方式得到预置体
    public GameObject goBullet;
    private GameObject bullet;

    //加载物体:用资源加载方式得到预置体,这种方式下资源要放在Assets/Resources文件夹下
    private GameObject mBullet;
    private GameObject myBullet;

    // Use this for initialization
    void Start () {
        mBullet = Resources.Load("Bullet") as GameObject;
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Fire1"))
        {
            //让预置体生成在场景中
            bullet = Instantiate(goBullet);
            bullet.transform.parent = this.transform;
        } else if(Input.GetButtonDown("Fire2")) {
            myBullet = Instantiate(mBullet);
            myBullet.transform.parent = this.transform;
        }
    }
}

Bullet:

using UnityEngine;

public class Bullet : MonoBehaviour {

    Vector3 fwd;

    // Use this for initialization
    void Start () {
        //向前向量
        fwd = transform.TransformDirection(Vector3.forward);
    }
    
    // Update is called once per frame
    void Update () {
        //给一个向前的力,打出去
        GetComponent<Rigidbody>().AddForce(fwd * 1000);
    }
}

参数如图:

 

 

 

 

转载于:https://www.cnblogs.com/lurenjiashuo/p/unity3d-create-get-load-find-object.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值