1.设计思路:界面会在某个区域随机生成球形,并且当我们点击鼠标右键并且点中球形并且鼠标抬起,则会销毁球形。
2.创建球形脚本:
#region 模块信息 // ********************************************************************** // Copyright (C) 2018 The company name // // 文件名(File Name): Test_SceneController.cs // 作者(Author): Dean1874 // 创建时间(CreateTime): 2018-03-19 17:35:20 // 修改者列表(modifier): // 模块描述(Module description): Test级别的场景控制器 // // ********************************************************************** #endregion using System; using UnityEngine; public class Test_SceneController : MonoBehaviour { /// <summary> /// 创建球形的区域 /// </summary> private Transform transCreateSphereArea; /// <summary> /// 球形的父物体 /// </summary> private Transform sphereParent; /// <summary> /// 球形预设 /// </summary> private GameObject m_SpherePrefab; /// <summary> /// 球形的上限值 /// </summary> private int m_MaxCount = 10; /// <summary> /// 下次克隆时间 /// </summary> private float m_NextCloneTime = 0.0f; /// <summary> /// 当前箱子数量 /// </summary> private int m_CurrentCount = 0; private void Awake() { transCreateSphereArea = GameObject.Find("CreateSphereArea").GetComponent<Transform>(); sphereParent = GameObject.Find("SphereAll").GetComponent<Transform>(); } private void Start() { m_SpherePrefab = Resources.Load<GameObject>("Test/Sphere_00"); Debug.Log("m_SpherePrefab = " + m_SpherePrefab); } private void Update() { if (m_CurrentCount < m_MaxCount) { if (Time.time > m_NextCloneTime) { //克隆 Clone(); } } } private void Clone() { m_NextCloneTime = Time.time + 0.5f; GameObject objClone = Instantiate(m_SpherePrefab); objClone.transform.SetParent(sphereParent, false); objClone.transform.position = transCreateSphereArea.transform.TransformPoint(new Vector3(UnityEngine.Random.Range(-0.5f, 0.5f), 0, UnityEngine.Random.Range(-0.5f, 0.5f))); Test_BoxController test_BoxController = objClone.GetComponent<Test_BoxController>(); if (test_BoxController != null) { test_BoxController.OnHit = SphereHit; } m_CurrentCount += 1; } private void SphereHit(GameObject obj) { m_CurrentCount -= 1; Destroy(obj); } }
3.挂载在球形上的脚本
#region 模块信息 // ********************************************************************** // Copyright (C) 2018 The company name // // 文件名(File Name): Test_BoxController.cs // 作者(Author): Dean1874 // 创建时间(CreateTime): 2018-03-18 16:14:07 // 修改者列表(modifier): Dean1874 // 模块描述(Module description): 测试移动脚本 // // ********************************************************************** #endregion using UnityEngine; using System; public class Test_BoxController : MonoBehaviour { public Action<GameObject> OnHit; public void Hit() { if (OnHit != null) { OnHit(gameObject); } } }
4.角色控制脚本(部分)
#region 射线的运用 if (Input.GetMouseButtonUp(1)) { Ray ray = m_RoleCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Item"))) { //Destroy(hit.collider.gameObject); Test_BoxController boxController = hit.collider.GetComponent<Test_BoxController>(); if (boxController != null) { boxController.Hit(); } //Debug.Log(hit.collider.name); } } #endregion