//------------------------------------------------------------------------/ // CTOR //------------------------------------------------------------------------/ public GameObjectInformation(GameObject target) { // Set target this.target = target; // Set components this.fieldCount = 0; this.propertyCount = 0; Component[] targetComponents = target.GetComponents <Component>(); List <ComponentInformation> components = new List <ComponentInformation>(); for (int i = 0; i < targetComponents.Length; ++i) { Component component = targetComponents[i]; if (component == null) { throw new Exception($"The component at index {i} is null!"); } ComponentInformation componentInfo = new ComponentInformation(component); this.fieldCount += componentInfo.fieldCount; this.propertyCount += componentInfo.propertyCount; components.Add(componentInfo); } this.components = components.ToArray(); // Now cache member references this.CacheReferences(); }
//------------------------------------------------------------------------/ // CTOR //------------------------------------------------------------------------/ public MemberReference(MemberInfo member, ComponentInformation componentInfo, int index) { this.name = member.Name; this.componentName = componentInfo.name; //this.gameObjectName = componentInfo.gameObject.name; this.type = member.MemberType; this.memberIndex = index; this.Initialize(componentInfo); }
/// <summary> /// Verifies that the component references for this GameObject are still valid, /// returning false if any components were removed /// </summary> private Change ValidateComponents() { bool watchlistChanged = false; bool changed = false; // Check if any components are null foreach (var component in this.components) { if (component.component == null) { changed = true; if (component.hasWatchList) { watchlistChanged = true; } } else { if (component.valid) { changed |= component.Refresh(); } } } // Check for other component changes Component[] targetComponents = target.GetComponents <Component>(); changed |= this.numberofComponents != targetComponents.Length; // If there's noticeable changes, let's add any components that were not there before if (changed) { List <ComponentInformation> currentComponents = new List <ComponentInformation>(); Func <ComponentInformation, bool> validate = (ComponentInformation component) => { return(component.component != null); }; currentComponents.AddRangeFiltered(this.components, validate); // If there's no information for this component, let's add it foreach (var component in targetComponents) { ComponentInformation ci = currentComponents.Find(x => x.component == component); if (ci == null) { ci = new ComponentInformation(component); currentComponents.Add(ci); } } // Now update the list of components this.components = currentComponents.ToArray(); } if (changed) { if (watchlistChanged) { return(Change.ComponentsAndWatchList); } return(Change.Components); } // If any components were removed, or added, note the change return(Change.None); }
//------------------------------------------------------------------------/ // Methods //------------------------------------------------------------------------/ /// <summary> /// Initializes this member reference, linking it to the component it's part of. /// This needs to be done before attempting to retrieve the value /// </summary> /// <param name="componentInfo"></param> public void Initialize(ComponentInformation componentInfo) { this.componentInfo = componentInfo; this.initialized = true; }