protected virtual PreferenceItem InitializePreferenceItem (XmlNode item)
		{
			PreferenceItem result = new PreferenceItem ();
			result.ID = item.Attributes [idAttributeName].Value;
			result.Name = item.Attributes [nameAttributeName].Value;
			result.Value = item.Attributes [valueAttributeName].Value;
			result.Description = item.InnerText;

			return result;
		}
Exemple #2
0
        protected virtual PreferenceItem InitializePreferenceItem(XmlNode item)
        {
            PreferenceItem result = new PreferenceItem();

            result.ID          = item.Attributes [idAttributeName].Value;
            result.Name        = item.Attributes [nameAttributeName].Value;
            result.Value       = item.Attributes [valueAttributeName].Value;
            result.Description = item.InnerText;

            return(result);
        }
Exemple #3
0
        protected override PreferenceItemCollection InitializePreferences(string collectionName)
        {
            var root             = CurrentXmlPreferences.DocumentElement;
            var targetCollection = root.SelectSingleNode(String.Format("preferenceCollection[@name='{0}']", collectionName));

            if (targetCollection == null)
            {
                return(null);
            }

            PreferenceItemCollection result = new PreferenceItemCollection(collectionName);

            foreach (XmlNode item in targetCollection.ChildNodes)
            {
                PreferenceItem currentPreference = InitializePreferenceItem(item);
                currentPreference.CollectionName = collectionName;
                result.Add(currentPreference);
            }
            return(result);
        }
Exemple #4
0
        public void CanReadAddRemovePreferenceItem()
        {
            PreferencesProvider preferences = ProviderFactory.GetInstance <PreferencesFactory> (ProviderRepositoryFactory.Instance.Provider).GetDefaultProvider <PreferencesProvider> ();

            Assert.AreNotEqual(preferences.GetPreferences("appSetting").Count, 0);
            Assert.AreEqual(preferences.GetPreferences("appSetting").Count, 2);
            Assert.AreEqual(preferences.GetPreferences("appSetting") ["1"].ID, "1");
            Assert.AreEqual(preferences.GetPreferences("appSetting") ["1"].Description, "preference no 1");

            PreferenceItemCollection appSetting = preferences.GetPreferences("appSetting");
            PreferenceItem           newItem    = new PreferenceItem();

            newItem.ID          = "3";
            newItem.Description = "Percentage";
            newItem.Name        = "3";
            newItem.Value       = "3";

            appSetting.Add(newItem);

            preferences.SavePreferences(appSetting);

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(preferences.XmlFileName);

            var dataTypeNode = xmlDoc.DocumentElement.SelectSingleNode(String.Format("preferenceCollection[@name='{0}']", appSetting.CollectionName));

            Assert.AreEqual(dataTypeNode.ChildNodes.Count, 3);
            Assert.AreEqual(dataTypeNode.ChildNodes [2].Attributes ["name"].Value, "3");

            appSetting = preferences.GetPreferences("appSetting");
            appSetting.Remove(appSetting ["3"]);
            preferences.SavePreferences(appSetting);

            xmlDoc = new XmlDocument();
            xmlDoc.Load(preferences.XmlFileName);

            dataTypeNode = xmlDoc.DocumentElement.SelectSingleNode(String.Format("preferenceCollection[@name='{0}']", appSetting.CollectionName));
            Assert.AreEqual(dataTypeNode.ChildNodes.Count, 2);
            Assert.AreNotEqual(dataTypeNode.ChildNodes [1].Attributes ["name"].Value, "3");
        }
Exemple #5
0
		public void CanReadAddRemovePreferenceItem ()
		{
			PreferencesProvider preferences = ProviderFactory.GetInstance<PreferencesFactory> (ProviderRepositoryFactory.Instance.Provider).GetDefaultProvider<PreferencesProvider> ();

			Assert.AreNotEqual (preferences.GetPreferences ("appSetting").Count, 0);
			Assert.AreEqual (preferences.GetPreferences ("appSetting").Count, 2);
			Assert.AreEqual (preferences.GetPreferences ("appSetting") ["1"].ID, "1");
			Assert.AreEqual (preferences.GetPreferences ("appSetting") ["1"].Description, "preference no 1");

			PreferenceItemCollection appSetting = preferences.GetPreferences ("appSetting");
			PreferenceItem newItem = new PreferenceItem ();
			newItem.ID = "3";
			newItem.Description = "Percentage";
			newItem.Name = "3";
			newItem.Value = "3";

			appSetting.Add (newItem);

			preferences.SavePreferences (appSetting);

			var xmlDoc = new XmlDocument ();
			xmlDoc.Load (preferences.XmlFileName);

			var dataTypeNode = xmlDoc.DocumentElement.SelectSingleNode (String.Format ("preferenceCollection[@name='{0}']", appSetting.CollectionName));
			Assert.AreEqual (dataTypeNode.ChildNodes.Count, 3);
			Assert.AreEqual (dataTypeNode.ChildNodes [2].Attributes ["name"].Value, "3");

			appSetting = preferences.GetPreferences("appSetting");
			appSetting.Remove (appSetting ["3"]);
			preferences.SavePreferences (appSetting);

			xmlDoc = new XmlDocument ();
			xmlDoc.Load (preferences.XmlFileName);

			dataTypeNode = xmlDoc.DocumentElement.SelectSingleNode (String.Format ("preferenceCollection[@name='{0}']", appSetting.CollectionName));
			Assert.AreEqual (dataTypeNode.ChildNodes.Count, 2);
			Assert.AreNotEqual (dataTypeNode.ChildNodes [1].Attributes ["name"].Value, "3");


		}
Exemple #6
0
        public override void SavePreference(PreferenceItem preference)
        {
            var root = CurrentXmlPreferences.DocumentElement;
            //Get collection of preference.
            var targetCollection = root.SelectSingleNode(String.Format("preferenceCollection[@name='{0}']", preference.CollectionName));

            if (targetCollection == null)
            {
                targetCollection = CurrentXmlPreferences.CreateNode(XmlNodeType.Element, "preference", null);

                var nameAttr = CurrentXmlPreferences.CreateAttribute(nameAttributeName);
                nameAttr.Value = preference.CollectionName;

                targetCollection.Attributes.Append(nameAttr);

                CurrentXmlPreferences.AppendChild(targetCollection);
            }

            //TODO: update preference if any and reset cache


            CurrentXmlPreferences.Save(XmlFileName);
        }
Exemple #7
0
        /// <summary>
        /// Open Unity preferences window to a class decorated with PreferenceItem attribute
        /// </summary>
        /// <param name="targetClass"></param>
        public static void OpenPreference(Type targetClass)
        {
            // Find the assemblies needed to access internal Unity classes
            Type tEditorAssembly = null;
            Type tAssemblyHelper = null;

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (tEditorAssembly == null)
                {
                    var tempEditorAssembly = assembly.GetType("UnityEditor.EditorAssemblies");
                    if (tempEditorAssembly != null)
                    {
                        tEditorAssembly = tempEditorAssembly;
                    }
                }
                if (tAssemblyHelper == null)
                {
                    var tempAssemblyHelper = assembly.GetType("UnityEditor.AssemblyHelper");
                    if (tempAssemblyHelper != null)
                    {
                        tAssemblyHelper = tempAssemblyHelper;
                    }
                }
                if (tEditorAssembly != null && tAssemblyHelper != null)
                {
                    break;
                }
            }

            if (tEditorAssembly == null || tAssemblyHelper == null)
            {
                return;
            }

            var nonPublicStatic = BindingFlags.Static | BindingFlags.NonPublic;

            var loadedAssemblyProp = tEditorAssembly.GetProperty("loadedAssemblies", nonPublicStatic);

            IList loadedAssemblies = loadedAssemblyProp.GetValue(null, null) as IList;
            var   methodGetTypes   = tAssemblyHelper.GetMethod("GetTypesFromAssembly", nonPublicStatic);

            if (loadedAssemblies == null || methodGetTypes == null)
            {
                return;
            }

            int targetIndex         = -1;
            int totalCustomSections = 0;

            // This reconstructs PreferenceWindow.AddCustomSections() as reflection calls
            foreach (object loadedAssemblyObj in loadedAssemblies)
            {
                Assembly assembly = loadedAssemblyObj as Assembly;
                if (assembly == null)
                {
                    continue;
                }
                IList types = methodGetTypes.Invoke(null, new object[] { assembly }) as IList;
                if (types == null)
                {
                    continue;
                }

                foreach (object typeObj in types)
                {
                    Type type = typeObj as Type;
                    if (type == null)
                    {
                        continue;
                    }
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
                    {
                        PreferenceItem preferenceItem = Attribute.GetCustomAttribute((MemberInfo)method, typeof(PreferenceItem)) as PreferenceItem;
                        if (preferenceItem != null)
                        {
                            if (type == targetClass)
                            {
                                targetIndex = totalCustomSections;
                            }
                            totalCustomSections++;
                        }
                    }
                }
            }

            if (targetIndex < 0)
            {
                return;
            }

            // Opening preference window, taken from here
            // http://answers.unity3d.com/questions/473949/open-editpreferences-from-code.html
            var asm          = Assembly.GetAssembly(typeof(EditorWindow));
            var tPrefsWindow = asm.GetType("UnityEditor.PreferencesWindow");
            var methodShow   = tPrefsWindow.GetMethod("ShowPreferencesWindow", nonPublicStatic);

            methodShow.Invoke(null, null);

            // Need to wait a few frames to let preference window build itself
            int frameWait = 3;

            EditorApplication.CallbackFunction waitCallback = null;
            waitCallback = () =>
            {
                frameWait--;
                if (frameWait > 0)
                {
                    return;
                }
                EditorApplication.update -= waitCallback;

                var prefWindow = EditorWindow.GetWindow(tPrefsWindow);

                var nonPublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;

                var fieldSections = tPrefsWindow.GetField("m_Sections", nonPublicInstance);
                int sectionCount  = (fieldSections.GetValue(prefWindow) as IList).Count;
                int startIdx      = sectionCount - totalCustomSections;

                var propSectionIndex = tPrefsWindow.GetProperty("selectedSectionIndex", nonPublicInstance);
                propSectionIndex.SetValue(prefWindow, startIdx + targetIndex, null);
            };

            EditorApplication.update += waitCallback;
        }
		public override void SavePreference (PreferenceItem preference)
		{
			var root = CurrentXmlPreferences.DocumentElement;
			//Get collection of preference.
			var targetCollection = root.SelectSingleNode (String.Format ("preferenceCollection[@name='{0}']", preference.CollectionName));

			if (targetCollection == null) {
				targetCollection = CurrentXmlPreferences.CreateNode (XmlNodeType.Element, "preference", null);

				var nameAttr = CurrentXmlPreferences.CreateAttribute (nameAttributeName);
				nameAttr.Value = preference.CollectionName;

				targetCollection.Attributes.Append (nameAttr);

				CurrentXmlPreferences.AppendChild (targetCollection);
			} 
			
			//TODO: update preference if any and reset cache
			
			
			CurrentXmlPreferences.Save (XmlFileName);
		}
		public override void RemovePreference (PreferenceItem preference)
		{
			//TODO: implement remove preference and reset cache
			throw new System.NotImplementedException();
		}
Exemple #10
0
 public override void RemovePreference(PreferenceItem preference)
 {
     //TODO: implement remove preference and reset cache
     throw new System.NotImplementedException();
 }