public static bool IsDifferent(Texture2DArrayManager mng) { return (mng.texture2DArray.depth != mng.textureInputArray.Length || (mng.textureInputArray.Length > 0 ? mng.texture2DArray.format != mng.textureInputArray[0].format : false) || !Array.TrueForAll(mng.textureInputArray, (tex) => tex.width == mng.texture2DArray.width && tex.height == mng.texture2DArray.height)); }
void OnEnable() { Texture2DArrayManager manager = targetAs; if (manager.texture2DArray == null && manager != null) { manager.texture2DArray = AddTexture2DArrayAt(target); } }
public override void OnInspectorGUI() { serializedObject.UpdateIfDirtyOrScript(); Texture2DArrayManager manager = targetAs; EditorGUI.BeginDisabledGroup(true); EditorGUILayout.ObjectField("Script", MonoScript.FromScriptableObject(manager), typeof(MonoScript), false); EditorGUI.EndDisabledGroup(); SerializedProperty textureProperty = serializedObject.FindProperty("textureInputArray"); EditorGUILayout.PropertyField(textureProperty, true); EditorGUILayout.Space(); if (GUILayout.Button("Refresh")) { ApplyTexture2DArray(manager); } serializedObject.ApplyModifiedProperties(); }
public static bool ApplyTexture2DArray(Texture2DArrayManager manager) { Texture2D[] tex2DArray = Array.FindAll(manager.textureInputArray, tex => tex != null); if (tex2DArray.Length <= 0) { errorBuilder.Remove(0, errorBuilder.Length); Array.ForEach(tex2DArray, (tex) => errorBuilder.Append(tex.name).Append(" : ").Append(tex.width).Append(',').Append(tex.height).Append('\n')); Debug.LogErrorFormat("Fail to apply.. not exist texture in array."); return(false); } Texture2D firstTex = tex2DArray[0]; int width = firstTex.width, height = firstTex.height; if (!Array.TrueForAll(tex2DArray, (tex) => tex.width == width && tex.height == height)) { errorBuilder.Remove(0, errorBuilder.Length); Array.ForEach(tex2DArray, (tex) => errorBuilder.Append(tex.name).Append(" : ").Append(tex.width).Append(',').Append(tex.height).Append('\n')); Debug.LogErrorFormat("Fail to apply.. all texture size must be same.\n{0}", errorBuilder.ToString()); return(false); } TextureFormat format = firstTex.format; if (!Array.TrueForAll(tex2DArray, (tex) => tex.format == format)) { format = TextureFormat.RGBA32; errorBuilder.Remove(0, errorBuilder.Length); Array.ForEach(tex2DArray, (tex) => errorBuilder.Append(tex.name).Append(" : ").Append(tex.format).Append('\n')); Debug.LogWarningFormat("All texture format is not same. Force {0}.{1} \n{2}", format.GetType().ToString(), format.ToString(), errorBuilder.ToString()); } Texture2DArray realArray = manager.texture2DArray; if (IsDifferent(manager)) { DestroyImmediate(manager.texture2DArray, true); realArray = new Texture2DArray(width, height, tex2DArray.Length, format, false); realArray.name = "Texture2DArray"; AssetDatabase.AddObjectToAsset(realArray, manager); manager.texture2DArray = realArray; Debug.LogWarning("Texture2DArray instance is re-created."); } for (int i = 0; i < tex2DArray.Length; i++) { for (int j = 0; j < tex2DArray[i].mipmapCount; j++) { Graphics.CopyTexture(tex2DArray[i], 0, j, realArray, i, j); } } realArray.Apply(true); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(manager)); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); return(true); }