//------------------------------------------------------------------------- // Parameter Store Part //------------------------------------------------------------------------- //------------------------------------------------------------------------- public static ColliderGenTK2DParameterStore EnsureParameterStorePrefabExistsForCollection(ref string parameterStoreSavePrefabPath, object spriteCollection) { ColliderGenTK2DParameterStore parameterStoreObject = null; parameterStoreSavePrefabPath = GetParameterStorePrefabFilePath(spriteCollection); string prefabDir = System.IO.Path.GetDirectoryName(parameterStoreSavePrefabPath); System.IO.FileInfo fileInfo = new System.IO.FileInfo(prefabDir); if (!fileInfo.Directory.Exists) { Debug.LogError("Directory '" + prefabDir + "' for creating the ColliderGenTK2DParameterStore prefab does not exist."); return(null); } parameterStoreObject = UnityEditor.AssetDatabase.LoadAssetAtPath(parameterStoreSavePrefabPath, typeof(ColliderGenTK2DParameterStore)) as ColliderGenTK2DParameterStore; // Does not exist yet - create if (parameterStoreObject == null) { GameObject go = new GameObject(); go.AddComponent <ColliderGenTK2DParameterStore>(); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) UnityEngine.Object p = EditorUtility.CreateEmptyPrefab(targetParameterStorePrefabPath); EditorUtility.ReplacePrefab(go, p); #else UnityEngine.Object p = UnityEditor.PrefabUtility.CreateEmptyPrefab(parameterStoreSavePrefabPath); PrefabUtility.ReplacePrefab(go, p); #endif GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); parameterStoreObject = UnityEditor.AssetDatabase.LoadAssetAtPath(parameterStoreSavePrefabPath, typeof(ColliderGenTK2DParameterStore)) as ColliderGenTK2DParameterStore; } return(parameterStoreObject); }
//------------------------------------------------------------------------- protected ColliderGenTK2DParameterStore CreateOrLoadParameterStore(ref string parameterStoreSavePrefabPath) { object spriteCollection = GetSelectedSpriteCollection(); ColliderGenTK2DParameterStore result = EnsureParameterStorePrefabExistsForCollection(ref parameterStoreSavePrefabPath, spriteCollection); result.UpdateToCurrentVersionIfNecessary(); return(result); }
//------------------------------------------------------------------------- protected void CheckForValuesToUpdate() { if (!mWasInitialized) { InitWithPreferencesValues(); } // The following actions are performed: // - storeNeedsReload = collection != oldCollection -> load store from prefab // - loadDifferentSpriteOfSameCollection = collection == oldCollection && spriteIDs != oldSpriteIDs -> load params from store (not from prefab) // // - PrepareHelperBackendForGui -> updates holes/islands data and sets up region-parameters if (mSpriteCollectionProxyToEdit != null && mSpriteIDsToEdit != null) { // SpriteCollection editor window is already open, sprites are selected. bool activeSpriteCollectionChanged = (mSpriteCollectionProxyToEdit != mOldSpriteCollectionProxyToEdit); bool spriteSelectionIDsChanged = !AreArraysEqual(mSpriteIDsToEdit, mOldSpriteIDsToEdit); bool storeNeedsReload = (mParameterStore == null || activeSpriteCollectionChanged); if (storeNeedsReload) { mParameterStore = CreateOrLoadParameterStore(ref mParameterStoreSaveFilePath); if (mParameterStore != null) { bool parametersFound = LoadValuesFromParameterStore(mParameterStore, mSpriteIDsToEdit); if (!parametersFound) { InitWithPreferencesValues(); } } mAtlasPathToMonitorForCommit = GetAtlasPathForActiveSpriteCollection(); } else { bool loadDifferentSpriteOfSameCollection = (spriteSelectionIDsChanged && !activeSpriteCollectionChanged); if (loadDifferentSpriteOfSameCollection) { bool parametersFound = LoadValuesFromParameterStore(mParameterStore, mSpriteIDsToEdit); if (!parametersFound) { InitWithPreferencesValues(); } } } if ((activeSpriteCollectionChanged || spriteSelectionIDsChanged)) { mAllSelectedSpritesHaveSameNumColliderRegions = PrepareHelperBackendForGui(mSpriteCollectionProxyToEdit, mSpriteIDsToEdit); } } }
//------------------------------------------------------------------------- protected void SaveValuesToParameterStore(ColliderGenTK2DParameterStore parameterStore, int[] spriteIDs) { if (spriteIDs.Length == 0 || parameterStore == null) { return; } foreach (int spriteID in spriteIDs) { ColliderGenTK2DParametersForSprite parameters = new ColliderGenTK2DParametersForSprite(); parameters.mRegionIndependentParameters = mAlgorithmHelper.RegionIndependentParams; parameters.mColliderRegionParameters = mAlgorithmHelper.ColliderRegionParams; parameters.mSpriteIndex = spriteID; parameterStore.SaveParametersForSprite(spriteID, parameters); } }
//------------------------------------------------------------------------- protected bool LoadValuesFromParameterStore(ColliderGenTK2DParameterStore parameterStore, int[] spriteIDs) { if (spriteIDs.Length == 0 || parameterStore == null) { return(false); } ColliderGenTK2DParametersForSprite parameters = parameterStore.GetParametersForSprite(spriteIDs[0]); if (parameters == null) { return(false); } mAlgorithmHelper.RegionIndependentParams = parameters.mRegionIndependentParameters; mAlgorithmHelper.ColliderRegionParams = parameters.mColliderRegionParameters; return(true); }
//------------------------------------------------------------------------- public static void SaveParameterStoreToPrefab(ColliderGenTK2DParameterStore parameterStore, string targetParameterStorePrefabPath) { GameObject go = new GameObject(); go.AddComponent <ColliderGenTK2DParameterStore>(); ColliderGenTK2DParameterStore emptyComponent = go.GetComponent <ColliderGenTK2DParameterStore>(); //EditorUtility.CopySerialized(parameterStore, emptyComponent); emptyComponent.CopyFrom(parameterStore); #if (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4) UnityEngine.Object p = EditorUtility.CreateEmptyPrefab(targetParameterStorePrefabPath); EditorUtility.ReplacePrefab(go, p); #else UnityEngine.Object p = UnityEditor.PrefabUtility.CreateEmptyPrefab(targetParameterStorePrefabPath); PrefabUtility.ReplacePrefab(go, p); #endif GameObject.DestroyImmediate(go); AssetDatabase.SaveAssets(); }
//------------------------------------------------------------------------- public void CopyFrom(ColliderGenTK2DParameterStore src) { this.mStoredParameters = src.mStoredParameters; }
//------------------------------------------------------------------------- public void CopyFrom(ColliderGenTK2DParameterStore src) { this.mStoredParameters = src.mStoredParameters; }