You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.8 KiB
140 lines
3.8 KiB
4 years ago
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text;
|
||
|
using UnityEngine;
|
||
|
|
||
|
[Serializable]
|
||
|
[DisallowMultipleComponent]
|
||
|
public partial class UIdSystem : MonoBehaviour
|
||
|
{
|
||
|
public bool IsPrefab;
|
||
|
public ulong Id;
|
||
|
|
||
|
[SerializeField]
|
||
|
private List<ComponentIdPair> componentIdPairs = new List<ComponentIdPair>();
|
||
|
|
||
|
/// <summary>
|
||
|
/// 为该实体对象和组件重新分配唯一的 Id。
|
||
|
/// </summary>
|
||
|
public void CreateId(ulong offset = 0, bool recursive = true)
|
||
|
{
|
||
|
this.Id = GUID.NewGuid() - offset;
|
||
|
|
||
|
var components = this.gameObject.GetComponents<Component>();
|
||
|
|
||
|
foreach (var component in components)
|
||
|
{
|
||
|
var index = GetComponentIndex(component);
|
||
|
|
||
|
if (index == -1)
|
||
|
{
|
||
|
var componentIdPair = new ComponentIdPair(component, GUID.NewGuid() - offset);
|
||
|
componentIdPairs.Add(componentIdPair);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
var componentIdPair = componentIdPairs[index];
|
||
|
componentIdPair.Id = GUID.NewGuid() - offset;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (recursive)
|
||
|
CreateIdRecursively(offset);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 为该实体对象和组件重新分配唯一 Id,已分配过的组件不会再分配。
|
||
|
/// </summary>
|
||
|
public void ReassignId(ulong offset = 0, bool recursive = true)
|
||
|
{
|
||
|
this.Id = GUID.NewGuid() - offset;
|
||
|
|
||
|
var components = this.gameObject.GetComponents<Component>();
|
||
|
|
||
|
foreach (var component in components)
|
||
|
{
|
||
|
var index = GetComponentIndex(component);
|
||
|
|
||
|
if (index == -1)
|
||
|
{
|
||
|
var componentIdPair = new ComponentIdPair(component, GUID.NewGuid() - offset);
|
||
|
componentIdPairs.Add(componentIdPair);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (recursive)
|
||
|
AssignIdRecursively(offset, true);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 为该实体对象和组件更新唯一 Id,已分配过的组件不会再更新。
|
||
|
/// </summary>
|
||
|
public void UpdateId(ulong offset = 0, bool recursive = true)
|
||
|
{
|
||
|
if (this.Id <= 0)
|
||
|
this.Id = GUID.NewGuid() - offset;
|
||
|
|
||
|
var components = this.gameObject.GetComponents<Component>();
|
||
|
|
||
|
foreach (var component in components)
|
||
|
{
|
||
|
var index = GetComponentIndex(component);
|
||
|
|
||
|
if (index == -1)
|
||
|
{
|
||
|
var componentIdPair = new ComponentIdPair(component, GUID.NewGuid() - offset);
|
||
|
componentIdPairs.Add(componentIdPair);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (recursive)
|
||
|
AssignIdRecursively(offset, false);
|
||
|
}
|
||
|
|
||
|
private void AssignIdRecursively(ulong offset, bool reassign)
|
||
|
{
|
||
|
foreach (Transform child in transform)
|
||
|
{
|
||
|
var entity = child.gameObject;
|
||
|
|
||
|
var component = entity.GetComponent<UIdSystem>();
|
||
|
if (component == null)
|
||
|
component = entity.AddComponent<UIdSystem>();
|
||
|
|
||
|
component.IsPrefab = this.IsPrefab;
|
||
|
|
||
|
if (reassign)
|
||
|
component.ReassignId(offset, true);
|
||
|
else
|
||
|
component.UpdateId(offset, true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void CreateIdRecursively(ulong offset)
|
||
|
{
|
||
|
foreach (Transform child in transform)
|
||
|
{
|
||
|
var entity = child.gameObject;
|
||
|
|
||
|
var component = entity.GetComponent<UIdSystem>();
|
||
|
if (component == null)
|
||
|
component = entity.AddComponent<UIdSystem>();
|
||
|
|
||
|
component.IsPrefab = this.IsPrefab;
|
||
|
component.CreateId(offset, true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private int GetComponentIndex(Component component)
|
||
|
{
|
||
|
return componentIdPairs.FindIndex((componentIdPair) => componentIdPair.Component == component);
|
||
|
}
|
||
|
|
||
|
private void Reset()
|
||
|
{
|
||
|
IsPrefab = false;
|
||
|
|
||
|
this.ReassignId();
|
||
|
}
|
||
|
}
|