zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Unity 工具 之 简单引用计数器的封装整理,继承即可使用,便于简单计数使用及相关计算事件触发

事件封装计算工具继承 简单 相关 整理
2023-09-11 14:20:50 时间

 

Unity 工具 之 简单引用计数器的封装整理,继承即可使用,便于简单计数使用及相关计算事件触发

 

目录

Unity 工具 之 简单引用计数器的封装整理,继承即可使用,便于简单计数使用及相关计算事件触发

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

五、实现步骤

六、关键代码


 

一、简单介绍

Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。

本节介绍,在 Unity 开发中,进行计数器的封装,与使用,方便后期开发,如果你有更好的方法,欢迎留言,多谢。

 

二、实现原理

1、接口,基类实现基本的计数功能,并添加相关的事件触发

2、本节案例,介绍一个房间进人开灯,没人关灯的案例,来介绍简单介绍计数器的使用

 

三、注意事项

1、接口,和基类不唯一,可以根据自己的需要重新整理

2、OnRetainFromZero() 和 OnReleaseToZero() 函数根据自己需要可以抽象,也可以虚有,或者也可以增加其他数量监控的事件

 

四、效果预览

 

五、实现步骤

1、打开 Unity,新建一个空工程

 

2、简单布置一下场景,添加一个 Plane,作为房间地面,并先关闭 Ligiht

 

3、在工程中添加脚本,BaseRefCounter 构建计数器的接口和基类,LightSwitch 等的开关类,RoomLightManager 管理房间进人的灯开关,TestRoomLightManager 模拟人进入和离开情景

 

4、把脚本 TestRoomLightManager 挂载到场景中,并对应赋值

 

5、运行场景,效果如上

 

六、关键代码

1、BaseRefCounter

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Tool_XAN {

	public interface IRefCounter { 
		int RefCount { get; }
		void Retain(object refOwner=null);
		void Release(object refOwner=null);
	}

	public class BaseRefCounter: IRefCounter
	{
        public int RefCount { get; private set; }

        public void Retain(object refOwner = null)
        {
            if (RefCount==0)
            {
                OnRetainFromZero();
            }
            RefCount++;
        }

        public void Release(object refOwner = null)
        {
            RefCount--;
            if (RefCount==0)
            {

                OnReleaseToZero();
            }
        }

        protected virtual void OnRetainFromZero() { }
        protected virtual void OnReleaseToZero() { }
    }
}

 

2、LightSwitch

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Example_XAN
{ 

	public class LightSwitch 
	{
		private Light mLight;
		public LightSwitch(Light light) {
			mLight = light;
		}

		public void Open() {
			mLight.enabled = true;
			Debug.Log("灯打开了……");
		}

		public void Close()
		{
			mLight.enabled = false;
			Debug.Log("灯关闭了……");
		}
	}
}

 

3、RoomLightManager

using System.Collections;
using System.Collections.Generic;
using Tool_XAN;
using UnityEngine;

namespace Example_XAN { 

	public class RoomLightManager : BaseRefCounter
	{
		LightSwitch mLightSwitch;
		List<GameObject> peopleLst;
		public RoomLightManager(LightSwitch lightSwitch) {
			mLightSwitch = lightSwitch;
			peopleLst = new List<GameObject>();
		}

		public void EnterPeople() {

			GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
			go.transform.position = RandomPos();
			peopleLst.Add(go);

			Retain();
			Debug.Log("有人进入房间,目前房间人数为:"+RefCount);
		}

		public void LeavePeople() {
			GameObject go = peopleLst[Random.Range(0, peopleLst.Count)];
			peopleLst.Remove(go);
			GameObject.Destroy(go);


			Release();
			Debug.Log("有人离开房间,目前房间人数为:" + RefCount);
		}

        protected override void OnRetainFromZero()
        {
			mLightSwitch.Open();

		}

        protected override void OnReleaseToZero()
        {
			mLightSwitch.Close();
		}

		private Vector3 RandomPos() {
			int x = Random.Range(-3,3);
			int z = Random.Range(-3,3);
			return new Vector3(x,1,z);
		}
    }
}

 

4、TestRoomLightManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Example_XAN
{ 

	public class TestRoomLightManager : MonoBehaviour
	{
		public Light Light;
		// Start is called before the first frame update
		IEnumerator Start()
		{
			RoomLightManager room = new RoomLightManager(new LightSwitch(Light));

			yield return new WaitForSeconds(0.5f);
			room.EnterPeople();

			yield return new WaitForSeconds(0.5f);
			room.LeavePeople();

			yield return new WaitForSeconds(1f);
			room.EnterPeople();

			yield return new WaitForSeconds(0.5f);
			room.EnterPeople();

			yield return new WaitForSeconds(0.5f);
			room.LeavePeople();

			yield return new WaitForSeconds(0.5f);
			room.LeavePeople();
		}

		
	}
}