Last active
August 16, 2019 12:29
-
-
Save am1tanaka/a02918371dd0bcd050fd2c7ef776b47f to your computer and use it in GitHub Desktop.
ProTexのImageをバインドするためのスクリプト
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace ProTex | |
{ | |
#if UNITY_EDITOR | |
[ExecuteInEditMode] | |
#endif | |
public class ProTexImageBinder : ProTexMaterialBinder | |
{ | |
void Start() | |
{ | |
if (proTexTexture != null) | |
{ | |
var image = gameObject.GetComponent<Image>(); | |
if (image != null) | |
{ | |
UpdateSourceImage(image, runtimeTextureSize); | |
} | |
else | |
{ | |
Debug.LogError("No image detected. GameObject [" + gameObject.name + "]"); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
public void SetEditorPreviewImage() | |
{ | |
if (proTexTexture != null) | |
{ | |
var image = gameObject.GetComponent<Image>(); | |
if (image == null) | |
{ | |
Debug.LogError("No image detected. GameObject [" + gameObject.name + "]"); | |
} | |
else | |
{ | |
UpdateSourceImage(image, runtimeTextureSize); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------------------ | |
void UpdateSourceImage(Image img, int textureSize) | |
{ | |
bool hasTexture = proTexTexture.HasTexture(TextureType.Color); | |
if (hasTexture) | |
{ | |
Texture2D tex = proTexTexture.GenerateTexture(textureSize, textureSize, TextureType.Color); | |
Sprite sp = Sprite.Create(tex, new Rect(0, 0, textureSize, textureSize), Vector2.zero); | |
img.sprite = sp; | |
} | |
} | |
#if UNITY_EDITOR | |
private void OnEnable() | |
{ | |
Start(); | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment