添加底层基础类(所有类的总管理器,致使objectmanager实现所有的生命周期的控制)

pull/1/head
zhangxl 2024-07-19 10:54:59 +08:00
parent 0c8cbe9329
commit bec26a432b
13 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ffc6016e330b134429d2d8d6286a541d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,64 @@
using UnityEngine;
namespace ZC
{
public class GameObjectBase<T> : IBehaviour, ICreateBindingGo
{
private GameObject _go;
private long _id;
private bool _isDisposed;
private bool _isPause;
public GameObject go => this._go;
public long Id => this._id;
public bool isDisposed => this._isDisposed;
public bool isPause => this._isPause;
protected GameObjectBase(GameObject go = null)
{
this._id = IDGenerator.Generate();
Debug.Log($"name is {typeof(T)}, id is {this._id}");
if (go == null)
{
var name = typeof(T).FullName;
this._go = new GameObject(name);
_go.transform.SetParent(ZCGame.ObjectPool);
this._go.name = name;
}
else
this._go = go;
}
public void SetGo(GameObject go)
{
this._go = go;
}
public virtual void OnInit()
{
this._isDisposed = false;
this._isPause = false;
}
public virtual void OnUpdate(float dateTime)
{
}
public virtual void OnPause()
{
this._isPause = true;
}
public virtual void OnResume()
{
this._isPause = false;
}
public virtual void OnDispose()
{
this._isDisposed = true;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 33ec6777b2ce4e50994218a7cd9b7d35
timeCreated: 1721114843

View File

@ -0,0 +1,18 @@
using System.Collections.Generic;
using UnityEngine;
namespace ZC
{
public static class GameObjectFactory
{
// public static T Create<T>(GameObject go = null) where T : GameObjectBase
// {
// var t = new T();
// var id = t.GetHashCode();
//
// t.OnInit();
// ZCGame.ObjectManager.Add(t);
// return t;
// }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 65a3597d68994c0fb9a1e37a1b60515e
timeCreated: 1721117317

View File

@ -0,0 +1,54 @@
using System;
namespace ZC
{
/// <summary>
/// id生成器当前使用的是时间戳作为id生成器的唯一id
/// </summary>
public static class IDGenerator
{
//
private static long lastTimestamp = -1L;
private static long sequence = 0L;
private static readonly long twepoch = 1288834974657L; // Twitter Snowflake算法中的自定义起始时间
private static readonly long machineId = 1L; // 机器码,可以自定义
public static long Generate()
{
long timestamp = TimeGen();
if (lastTimestamp == timestamp)
{
sequence = (sequence + 1) & 4095L;
if (sequence == 0)
{
timestamp = TilNextMillis(lastTimestamp);
}
}
else
{
sequence = 0L;
}
lastTimestamp = timestamp;
return (timestamp - twepoch) << 22 | machineId << 12 | sequence;
}
private static long TilNextMillis(long lastTimestamp)
{
long timestamp = TimeGen();
while (timestamp <= lastTimestamp)
{
timestamp = TimeGen();
}
return timestamp;
}
private static long TimeGen()
{
return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 468958ca434443d79f1bf408bca3bf58
timeCreated: 1721350450

View File

@ -0,0 +1,26 @@
using UnityEngine;
namespace ZC
{
public interface ICreateBindingGo
{
GameObject go { get; }
void SetGo(GameObject go);
}
public interface IBehaviour
{
long Id { get; }
bool isDisposed { get; }
bool isPause { get; }
void OnInit();
void OnUpdate(float dateTime);
void OnPause();
void OnResume();
void OnDispose();
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a146ca6ae4bc4923887b45b0488f8beb
timeCreated: 1721114797

View File

@ -0,0 +1,13 @@
using UnityEngine;
namespace ZC
{
public interface IManager
{
}
internal abstract class ManagerBase<T> : GameObjectBase<T>, IManager
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b4980a297cb34d8e85b4f80115b3e206
timeCreated: 1721114891

View File

@ -0,0 +1,76 @@
using System.Collections.Generic;
using UnityEngine;
namespace ZC
{
public interface IObjectManager
{
void Add(IBehaviour behaviour);
void Remove(long id);
void Clear();
}
internal class ObjectManager : ManagerBase<ObjectManager>, IObjectManager
{
private Dictionary<long, IBehaviour> pool;
public override void OnInit()
{
base.OnInit();
this.pool = new Dictionary<long, IBehaviour>();
}
public override void OnUpdate(float time)
{
base.OnUpdate(time);
foreach (var behaviour in this.pool.Values)
{
if (behaviour.isDisposed)
{
this.pool.Remove(behaviour.Id);
continue;
}
behaviour.OnUpdate(time);
}
Debug.Log(this.pool.Count);
}
public override void OnDispose()
{
base.OnDispose();
this.Clear();
}
#region Dic
public void Add(IBehaviour behaviour)
{
behaviour.OnInit();
this.pool.Add(behaviour.Id, behaviour);
}
public void Remove(long id)
{
if (this.pool.TryGetValue(id, out var behaviour))
{
behaviour?.OnDispose();
}
this.pool.Remove(id);
}
public void Clear()
{
foreach (var value in this.pool.Values)
{
value.OnDispose();
}
this.pool.Clear();
}
#endregion
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 13fd1962ffff41de9804bc3dc697d1bf
timeCreated: 1721116305