/// <summary> /// Clear this instance. /// </summary> public override void Clear() { Attribute = null; Value = null; base.Clear(); }
/// <summary> /// Adds the attribute and returns it. /// </summary> /// <param name="rID">Attribute ID to add</param> /// <param name="rValue">Value to add to the attribute</param> /// <returns>Attribute that matches the ID</returns> public virtual BasicAttribute AddAttribute <T>(string rID, T rValue = default(T)) { BasicAttribute lItem = AddAttribute(rID, typeof(T)); lItem.SetValue <T>(rValue); return(lItem); }
/// <summary> /// Removes the specified attribute from the list of items /// </summary> /// <param name="rAttribute"></param> public virtual void RemoveAttribute(BasicAttribute rItem) { if (!mItemCache.ContainsKey(rItem.ID)) { return; } rItem.Attributes = null; rItem.IsValid = false; mItems.Remove(rItem); mItemCache.Remove(rItem.ID); }
/// <summary> /// Renames the attribute and fixes the cache /// </summary> /// <param name="rOldName">Old name of the attribute to change from.</param> /// <param name="rNewName">New name of the attribute to change to.</param> public virtual void RenameAttribute(string rOldName, string rNewName) { if (!mItemCache.ContainsKey(rOldName)) { return; } BasicAttribute lItem = mItemCache[rOldName]; lItem._ID = rNewName; mItemCache.Remove(rOldName); mItemCache.Add(rNewName, lItem); }
/// <summary> /// Due to Unity's serialization limits, we have to do this ourselves: /// 1. GameObjects don't allow for lists of derived types /// 2. You can use ScriptableObjects, but prefabs don't support ScriptableObjects /// 3. ISerializationCallbackReceiver can't use Find() /// /// This function is called AFTER the items have been deserialized /// </summary> public void OnAfterDeserialize() { JSONSerializer.RootObject = gameObject; for (int i = 0; i < mItemDefinitions.Count; i++) { Type lType = null; JSONNode lNode = JSONNode.Parse(mItemDefinitions[i]); if (lNode != null) { string lTypeString = lNode["__Type"].Value; if (lTypeString != null && lTypeString.Length > 0) { lType = Type.GetType(lTypeString); } } // Fill an existing existing attribute if (lType != null && mItems.Count > i && mItems[i].GetType() == lType) { object lItemObj = mItems[i]; JSONSerializer.DeserializeInto(mItemDefinitions[i], ref lItemObj); } // Create a new attribute else { BasicAttribute lAttribute = JSONSerializer.Deserialize(mItemDefinitions[i]) as BasicAttribute; if (lAttribute != null) { if (mItems.Count <= i) { mItems.Add(lAttribute); } else { mItems[i] = lAttribute; } } } } // Get rid of excess items for (int i = mItems.Count - 1; i > mItemDefinitions.Count - 1; i--) { mItems.RemoveAt(i); } JSONSerializer.RootObject = null; }
/// <summary> /// Removes the specified attribute from the list of items /// </summary> /// <param name="rID">String representing the name or ID of the attribute we're removing</param> /// <param name="rAttribute"></param> public virtual void RemoveAttribute(string rID) { if (!mItemCache.ContainsKey(rID)) { return; } BasicAttribute lItem = mItemCache[rID]; lItem.Attributes = null; lItem.IsValid = false; mItems.Remove(lItem); mItemCache.Remove(rID); }
/// <summary> /// Adds the attribute if it doesn't exist and returns it. /// </summary> /// <param name="rAttributeID">Attribute ID to add (if it doesn't exist)</param> /// <returns>Attribute that matches the ID</returns> public virtual BasicAttribute AddAttribute(string rAttributeID) { // Return the existing attribute for (int i = Items.Count - 1; i >= 0; i--) { if (string.Compare(Items[i].ID, rAttributeID, true) == 0) { return(Items[i]); } } // Add the new attribute BasicAttribute lAttribute = new BasicAttribute(); lAttribute.ID = rAttributeID; Items.Add(lAttribute); return(lAttribute); }
/// <summary> /// Due to Unity's serialization limits, we have to do this ourselves: /// 1. GameObjects don't allow for lists of derived types /// 2. You can use ScriptableObjects, but prefabs don't support ScriptableObjects /// 3. ISerializationCallbackReceiver can't use Find() /// /// This function is called AFTER the items have been deserialized /// </summary> public void OnAfterDeserialize() { JSONSerializer.RootObject = gameObject; // CDL 07/27/2018 - keep track of any invalid attribute definitions List <int> lInvalidDefinitions = new List <int>(); for (int i = 0; i < mItemDefinitions.Count; i++) { Type lType = null; bool lUpdateType = false; JSONNode lNode = JSONNode.Parse(mItemDefinitions[i]); if (lNode != null) { string lTypeString = lNode["__Type"].Value; // CDL 07/27/2018 - Use AssemblyHelper.ResolveType() intead of Type.GetType() so that // the Type can still be obtained if the Motion was serialized as belonging to a different assembly lType = AssemblyHelper.ResolveType(lTypeString, out lUpdateType); //Debug.Log("Attribute type resolved to " + lType.AssemblyQualifiedName); //if (lTypeString != null && lTypeString.Length > 0) { lType = Type.GetType(lTypeString); } } // CDL 07/27/2018 - save this definition for removal afterwards, as we can't resolve the Type from any assembly. if (lType == null) { lInvalidDefinitions.Add(i); continue; } // Fill an existing existing attribute if (lType != null && mItems.Count > i && mItems[i].GetType() == lType) { object lItemObj = mItems[i]; JSONSerializer.DeserializeInto(mItemDefinitions[i], ref lItemObj); // CDL 07/27/2018 - update the serialized Type if necessary if (lUpdateType) { //Debug.Log("Updating type: " + lItemObj.GetType().AssemblyQualifiedName); mItemDefinitions[i] = JSONSerializer.Serialize(lItemObj, false); } } // Create a new attribute else { BasicAttribute lAttribute = JSONSerializer.Deserialize(mItemDefinitions[i]) as BasicAttribute; if (lAttribute != null) { if (mItems.Count <= i) { mItems.Add(lAttribute); } else { mItems[i] = lAttribute; } } } } // Get rid of excess items for (int i = mItems.Count - 1; i > mItemDefinitions.Count - 1; i--) { mItems.RemoveAt(i); } // CDL 07/27.2018 - Clean up any invalid attribute definitions if (lInvalidDefinitions.Count > 0) { for (int i = 0; i < lInvalidDefinitions.Count; i++) { int lIndex = lInvalidDefinitions[i]; mItemDefinitions.RemoveAt(lIndex); } } JSONSerializer.RootObject = null; }
/// <summary> /// Adds the attribute and returns it. /// </summary> /// <param name="rID">Attribute ID to add</param> /// <param name="rType">Type of attribute we'll add. It must be one of the supported types and null means tag.</param> /// <returns>Attribute that matches the ID</returns> public virtual BasicAttribute AddAttribute(string rID, Type rType = null) { // Ensure we have an ID if (rID.Length == 0) { rID = "Attribute"; } // Ensure we have a unique ID string lID = rID; int i = 0; while (mItemCache.ContainsKey(lID)) { i++; lID = rID + " (" + i + ")"; } // Add the attribute BasicAttribute lItem = null; Type lType = null; int lTypeIndex = EnumAttributeTypes.GetEnum(rType); if (lTypeIndex == 0) { lType = typeof(BasicAttributeTag); } if (lTypeIndex == 1) { lType = typeof(BasicAttributeString); } if (lTypeIndex == 2) { lType = typeof(BasicAttributeFloat); } if (lTypeIndex == 3) { lType = typeof(BasicAttributeInt); } if (lTypeIndex == 4) { lType = typeof(BasicAttributeBool); } if (lTypeIndex == 5) { lType = typeof(BasicAttributeVector2); } if (lTypeIndex == 6) { lType = typeof(BasicAttributeVector3); } if (lTypeIndex == 7) { lType = typeof(BasicAttributeVector4); } if (lTypeIndex == 8) { lType = typeof(BasicAttributeQuaternion); } if (lTypeIndex == 9) { lType = typeof(BasicAttributeTransform); } if (lTypeIndex == 10) { lType = typeof(BasicAttributeGameObject); } if (lType != null) { lItem = Activator.CreateInstance(lType) as BasicAttribute; // ScriptableObject.CreateInstance(lType) as BasicAttribute; lItem.Attributes = this; lItem._ID = lID; mItems.Add(lItem); mItemCache.Add(lID, lItem); } return(lItem); }
/// <summary> /// Removes the specified attribute from the list of items /// </summary> /// <param name="rAttribute"></param> public virtual void RemoveAttribute(BasicAttribute rAttribute) { Items.Remove(rAttribute); }