Created
July 24, 2019 06:29
-
-
Save tanakaedu/4d693a3b60b7ef09884d796622caeece to your computer and use it in GitHub Desktop.
ProTexImageBinder用のエディタースクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine; | |
namespace ProTex | |
{ | |
[CustomEditor(typeof(ProTexImageBinder))] | |
public class ProTexImageBinderInspector : UnityEditor.Editor | |
{ | |
//------------------------------------------------------------------------------------------------------------------ | |
private enum TextureSize | |
{ | |
TextureSize64x64 = 64, | |
TextureSize128x128 = 128, | |
TextureSize256x256 = 256, | |
TextureSize512x512 = 512, | |
TextureSize1024x1024 = 1024, | |
TextureSize2048x2048 = 2048 | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
private float progress; | |
//------------------------------------------------------------------------------------------------------------------ | |
public override void OnInspectorGUI() | |
{ | |
var proTexImageBinder = (ProTexImageBinder)target; | |
DrawProTexTextureField(proTexImageBinder); | |
DrawTextureSizeSelector(proTexImageBinder); | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
private void DrawProTexTextureField(ProTexImageBinder proTexMaterialBinder) | |
{ | |
if (Event.current.commandName == "ObjectSelectorUpdated") | |
{ | |
var newProTexTexture = EditorGUIUtility.GetObjectPickerObject() as ProTexTexture; | |
if (newProTexTexture != null) | |
{ | |
SetProTexTexture(proTexMaterialBinder, newProTexTexture); | |
} | |
} | |
else | |
{ | |
var oldProTexTexture = proTexMaterialBinder.proTexTexture; | |
var newProTexTexture = (ProTexTexture)EditorGUILayout.ObjectField( | |
"ProTexTexture", | |
oldProTexTexture, | |
typeof(ProTexTexture), | |
false); | |
if (newProTexTexture != oldProTexTexture) | |
{ | |
SetProTexTexture(proTexMaterialBinder, newProTexTexture); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
private void SetProTexTexture(ProTexImageBinder proTexMaterialBinder, ProTexTexture proTexTexture) | |
{ | |
if ((proTexTexture == null) || (proTexTexture.GetVersionNumber() <= ProTexTexture.VersionNumber)) | |
{ | |
proTexMaterialBinder.proTexTexture = proTexTexture; | |
proTexMaterialBinder.SetEditorPreviewImage(); | |
EditorSceneManager.MarkSceneDirty(proTexMaterialBinder.gameObject.scene); | |
} | |
else | |
{ | |
EditorUtility.DisplayDialog( | |
"Invalid texture version", | |
"The selected ProTexTexture version is newer than the current version", | |
"OK"); | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
private void DrawTextureSizeSelector(ProTexImageBinder proTexMaterialBinder) | |
{ | |
TextureSize textureSize = GetTextureSize(proTexMaterialBinder); | |
TextureSize newTextureSize = (TextureSize)EditorGUILayout.EnumPopup(textureSize); | |
if (textureSize != newTextureSize) | |
{ | |
proTexMaterialBinder.runtimeTextureSize = (int)newTextureSize; | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
private TextureSize GetTextureSize(ProTexImageBinder proTexMaterialBinder) | |
{ | |
int textureSize = proTexMaterialBinder.runtimeTextureSize; | |
foreach (var size in Enum.GetValues(typeof(TextureSize))) | |
{ | |
if (Convert.ToInt32(size) >= textureSize) | |
{ | |
return (TextureSize)size; | |
} | |
} | |
return TextureSize.TextureSize256x256; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment