Skip to content

Instantly share code, notes, and snippets.

@am1tanaka
Last active August 16, 2019 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am1tanaka/a02918371dd0bcd050fd2c7ef776b47f to your computer and use it in GitHub Desktop.
Save am1tanaka/a02918371dd0bcd050fd2c7ef776b47f to your computer and use it in GitHub Desktop.
ProTexのImageをバインドするためのスクリプト
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