Unity 用简单工厂模式创建对象

首先是要求:

  • 实现点击效果。
    • 用 Plane 或其他物体做地面, tag 为“Finish”
    • 点击地面后,出现一个圆形攻击标记,两秒后自动消失。注意:该攻击标记不能挡住点击。(Primitive Objects / Cylinder)
    • 请使用一个简单工厂创建、管理这些的标记,并自动收回这些标记(注意,这些对象创建后,放在列表内,不必释放)。

写代码的难点在于:

  1. 简单工厂其实是不难的,参考之前的代码可以很快写出
  2. 一个注意的地方在于射线的不遮挡
  3. 还有一个是延时调用的处理

全部代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Com.mygame;

namespace Com.mygame{
    public class myFactory : System.Object{
        public List<GameObject> objs = new List<GameObject>();
        private static myFactory _instance;
        private Camera cm;
        public Vector3 uselessposition =  new Vector3(0f,-2f,0f);

        public static myFactory GetInstance(){
            if (_instance == null) {
                _instance = new myFactory ();
            }
            return _instance;
        }

        public void placeAttackmark(Vector3 target){
            cm = Camera.main;
            Ray ray = cm.ScreenPointToRay (target);
            RaycastHit hit;
            if(Physics.Raycast(ray,out hit)){
                if (hit.collider.gameObject.tag.Contains ("Finish")) {
                    Vector3 finishposition = hit.point + new Vector3(0f,0.5f,0f);
                     //
                    if (objs.Count == 0) {
                        GameObject sp = GameObject.CreatePrimitive (PrimitiveType.Cylinder);
                        sp.GetComponent<Collider> ().enabled = false;
                        sp.transform.position = hit.point+ new Vector3(0f,0.5f,0f);;
                        // 在这里我给每一个新创建的对象添加控制延时的类,继承与monobehavier 
                        waitTime wt = sp.AddComponent<waitTime>();
                        // 给它添加延时函数,传入List,在延时函数中执行list的添加
                        wt.delayfunction (sp,objs);
                    } else {
                        GameObject sp = objs [0];
                        objs.RemoveAt (0);
                        sp.transform.position = hit.point + new Vector3(0f,0.5f,0f);
                        //add a timeclass
                        waitTime wt = sp.AddComponent<waitTime>();
                        wt.delayfunction (sp,objs);

                    }
                }
            }
        }

        //delay time
        public class waitTime : MonoBehaviour {
            public List<GameObject> objss;
            public GameObject obj;

            public void delayfunction(GameObject sp,List<GameObject> objs){
                StartCoroutine (Waitanddo(sp));
                this.objss = objs;
                this.obj = sp;
            }

            public IEnumerator Waitanddo(GameObject sp){
                yield return new WaitForSeconds(2.0f);
                //
                this.transform.position = new Vector3 (0f,-2f,0f);
                objss.Add (obj);
            }
        }
    }



}

public class basecode : MonoBehaviour {

    private Camera cm;
    private GameObject temp;
    // Use this for initialization
    void Start () {
        cm = Camera.main;
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            Vector3 mp = Input.mousePosition;
            myFactory.GetInstance ().placeAttackmark (mp);
        }
    }
}

你可能感兴趣的:(unity)