private UnityEngine.Object getValue(Rect position, SerializedProperty property, SerializedProperty idProp, SerializedProperty pathProp) { UnityEngine.Object obj = null; if (idProp.intValue > 0) { InstanceManager manager = (property.serializedObject.targetObject as Component).gameObject.scene.findInstance <InstanceManager>(); if (manager != null) { SavableInstance instance = manager.getInstanceById(idProp.intValue); if (!string.IsNullOrEmpty(pathProp.stringValue)) { GameObject child = instance.findChild(pathProp.stringValue); if (child != null) { if (_refType == typeof(GameObject)) { obj = child; } else { obj = child.GetComponent(_refType); } } } else { if (_refType == typeof(GameObject)) { obj = instance.gameObject; } else { obj = instance.GetComponent(_refType); } } } else { GUI.Box(position, "场景里没有" + nameof(InstanceManager)); obj = null; } } else if (!string.IsNullOrEmpty(pathProp.stringValue)) { Scene scene = (property.serializedObject.targetObject as Component).gameObject.scene; GameObject child = scene.findGameObjectAt(pathProp.stringValue); if (child != null) { if (_refType == typeof(GameObject)) { obj = child; } else { obj = child.GetComponent(_refType); } } } return(obj); }
public InstanceReference(Component component) { if (component != null) { path = component.gameObject.name; for (Transform parent = component.gameObject.transform.parent; parent != null; parent = parent.parent) { SavableInstance instance = parent.GetComponent <SavableInstance>(); if (instance == null) { path = parent.gameObject.name + '/' + path; } else { id = instance.id; break; } } } else { id = 0; path = ""; } }
public static SavableInstance create(Scene scene, string path, int id) { SavableInstance instance = scene.newGameObjectAt(path).AddComponent <SavableInstance>(); instance._id = id; return(instance); }
protected void Update() { if (manager != null) { if (id <= 0) { //没有注册,注册ID。 id = manager.allocate(this); _checked = true; } else if (!_checked) { //已经注册,没有检查,检查是否实际上丢失了注册。 SavableInstance other = manager.getInstanceById(id); if (other == null) { //有ID但是丢失引用,重新分配引用 manager.reallocate(id, this); } else if (other != this) { //引用被别人占据了,重新注册 id = manager.allocate(this); } } } }
public void dellocate(SavableInstance instance) { if (instance != null) { _data.idPool.Add(instance.id); int index = _registrations.FindIndex(e => { return(e.id == instance.id); }); if (index > 0) { _registrations.RemoveAt(index); } } }
public ILoadableData save() { cleanNullRegistrations(); //保存实例 _data.instances = new SavableInstanceData[_registrations.Count]; for (int i = 0; i < _data.instances.Length; i++) { SavableInstance instance = _registrations[i].instance; _data.instances[i] = new SavableInstanceData(instance.id, instance.path); } return(_data); }
private void resetValue(SerializedProperty idProp, SerializedProperty pathProp, UnityEngine.Object obj) { if (obj != null) { GameObject go = null; if (obj is GameObject) { go = (obj as GameObject); } else if (obj is Component) { go = (obj as Component).gameObject; } else { idProp.intValue = 0; pathProp.stringValue = string.Empty; return; } SavableInstance instance = go.GetComponentInParent <SavableInstance>(); if (instance != null) { idProp.intValue = instance.id; pathProp.stringValue = go.name; for (Transform parent = go.transform.parent; parent != null; parent = parent.parent) { if (parent != instance.transform) { pathProp.stringValue = parent.gameObject.name + '/' + pathProp.stringValue; } else { break; } } } else { idProp.intValue = 0; pathProp.stringValue = go.name; for (Transform parent = go.transform.parent; parent != null; parent = parent.parent) { pathProp.stringValue = parent.gameObject.name + '/' + pathProp.stringValue; } } } else { idProp.intValue = 0; pathProp.stringValue = string.Empty; } }
public int allocate(SavableInstance instance) { int id = 0; if (_data.idPool.Count > 0) { id = _data.idPool[_data.idPool.Count - 1]; _data.idPool.RemoveAt(_data.idPool.Count - 1); } else { id = _registrations.Count + 1; } _registrations.Add(new SavableInstanceRegistration(id, instance)); return(id); }
/// <summary> /// 重新分配ID /// </summary> /// <param name="id"></param> /// <param name="instance"></param> public void reallocate(int id, SavableInstance instance) { SavableInstanceRegistration r = _registrations.Find(e => { return(e.id == id); }); if (r != null) { r.instance = instance; } else { if (data.idPool.Remove(id)) { _registrations.Add(new SavableInstanceRegistration(id, instance)); } else { //从来都没分配过这样的ID,那么你说有就有吧。 _registrations.Add(new SavableInstanceRegistration(id, instance)); } } }
public T findInstanceIn <T>(InstanceManager manager) { if (id > 0) { SavableInstance instance = manager.getInstanceById(id); if (string.IsNullOrEmpty(path)) { return(instance.GetComponent <T>()); } else { string[] names = path.Split('/'); Transform child = instance.transform; for (int i = 0; i < names.Length; i++) { child = child.Find(names[i]); } if (child != null) { return(child.GetComponent <T>()); } else { return(default);
public SavableInstanceRegistration(int id, SavableInstance instance) { this.id = id; this.instance = instance; }