题目:
写一篇短文,描述以下游戏需求的实现。请写步骤,贴代码并解释:
实现点击效果。
- 用 Plane 或其他物体做地面, tag 为“Finish”
- 点击地面后,出现一个圆形攻击标记,两秒后自动消失。注意:该攻击标记不能挡住点击。(Primitive Objects / Cylinder)
- 请使用一个简单工厂创建、管理这些的标记,并自动收回这些标记(注意,这些对象创建后,放在列表内,不必释放)。
用户仅需申请使用即可
GameObject myFactory.placeAttackMark(Vector3 postion)
回答:
这次作业,部分代码参考了师兄的代码。
先上UMLet图
为了简化代码,我将本来应该是封装的私有变量,改为公有变量,如果需要保证数据的封装,只需要多加上一个公有的成员函数用来返回私有变量的值就行了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Com.Mygame;
namespace TimeTime {
public class TimeControl : MonoBehaviour
{
public GameMaster GameM;
private float time;
// Use this for initialization
void Start()
{
}
public void setting()
{
time = Time.time;
GameM = ActionManager.master;
}
// Update is called once per frame
void Update()
{
if (Time.time - time > 2.0f)
{
Debug.Log("Should be destroyed");
GameM.MarkComplete(this.gameObject);
Destroy(this);
}
}
}
}
using UnityEngine;
using System.Collections;
using Com.Mygame;
using System.Collections.Generic;
using TimeTime;
public class GameMaster : MonoBehaviour
{
public GameObject Mark;
private Vector3 UnusedPlace = new Vector3(1000f, 1000f, 1000f);
private List<GameObject> using_Symbol = new List<GameObject>();
private List<GameObject> unuse_Symbol = new List<GameObject>();
void Start()
{
ActionManager.master = this;
for (int i = 0; i < 3; i++)
{
GameObject newMark = GameObject.Instantiate(Mark, UnusedPlace, Quaternion.identity) as GameObject;
unuse_Symbol.Add(newMark);
}
}
public void AskForMark(Vector3 point)
{
if (unuse_Symbol.Count == 0)
{
GameObject newMark = GameObject.Instantiate(Mark, UnusedPlace, Quaternion.identity) as GameObject;
unuse_Symbol.Add(newMark);
}
GameObject OneObject = unuse_Symbol[0];
OneObject.transform.position = point;
TimeControl ac = OneObject.AddComponent<TimeControl>();
ac.setting();
unuse_Symbol.RemoveAt(0);
using_Symbol.Add(OneObject);
}
public void MarkComplete(GameObject obj)
{
using_Symbol.Remove(obj);
unuse_Symbol.Add(obj);
obj.transform.position = UnusedPlace;
}
}
using UnityEngine;
using System.Collections;
using Com.Mygame;
public class GameControl : MonoBehaviour
{
public Camera cam;
private ActionManager myFactory;
void Start()
{
Debug.Log("Camera can work sucessfully!");
myFactory = ActionManager.getInstance();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mp = Input.mousePosition;
Ray ray = cam.GetComponent<Camera>().ScreenPointToRay(mp);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.tag == "Finish")
{
myFactory.placeAttackMark(hit.point);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Com.Mygame
{
public class ActionManager : System.Object
{
private static ActionManager _instance;
public static GameMaster master;
public static ActionManager getInstance()
{
if (_instance == null)
{
_instance = new ActionManager();
}
return _instance;
}
public void placeAttackMark(Vector3 point)
{
Debug.Log("This is the Hit Position" + point);
//GameObject master = GameObject.Find("GameMaster");
master.AskForMark(point);
// showSymbol ac = mark.AddComponent<showSymbol>();
// ac.setting(point);
}
}
}