Advertisement
Guest User

Unity Icon Generator at runtime

a guest
Jun 12th, 2022
12
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class IconGenerator : MonoBehaviour
  7. {
  8.     [SerializeField] int _textureSize = 256;
  9.     [SerializeField] GameObject _targetObject;
  10.     [SerializeField] Camera _iconCam;
  11.     [SerializeField] Image _outputImage;
  12.     [SerializeField] LayerMask _layerMask;
  13.     [SerializeField] bool _destroyCopy = true;
  14.  
  15.     GameObject _copyObject;
  16.     Bounds _objectBounds;
  17.     RenderTexture _renderTex;
  18.  
  19.     IEnumerator _generateTextureCoroutine;
  20.  
  21.     private void Start() {
  22.         _renderTex = new RenderTexture(_textureSize, _textureSize, 0);
  23.         _iconCam.targetTexture = _renderTex;
  24.         _iconCam.cullingMask = _layerMask;
  25.     }
  26.  
  27.     public void OnClickGenerateIcon() {
  28.         Debug.Log("Generating new icon");
  29.  
  30.         if (_copyObject != null)
  31.             Destroy(_copyObject);
  32.  
  33.         _copyObject = Instantiate(_targetObject);
  34.  
  35.         SetCenterPoint();
  36.  
  37.         _copyObject.transform.SetParent(transform);
  38.         _copyObject.transform.localPosition = -_objectBounds.center;
  39.         _copyObject.transform.localRotation = Quaternion.identity;
  40.  
  41.         FitCameraToObjectBounds();
  42.  
  43.         int layer = LayerMask.NameToLayer("Icon Generator");
  44.         SetLayerToChildren(_copyObject, layer);
  45.  
  46.         // start coroutine to generate a sprite from a texture
  47.         if (_generateTextureCoroutine != null)
  48.             StopCoroutine(_generateTextureCoroutine);
  49.         _generateTextureCoroutine = GetSpriteFromTexture();
  50.         StartCoroutine(_generateTextureCoroutine);
  51.     }
  52.  
  53.     void SetCenterPoint() {
  54.         MeshRenderer[] renderers = _copyObject.GetComponentsInChildren<MeshRenderer>();
  55.  
  56.         _objectBounds = new Bounds(_copyObject.transform.position, Vector3.zero);
  57.  
  58.         for (int i = 0; i < renderers.Length; i++) {
  59.             _objectBounds.Encapsulate(renderers[i].bounds);
  60.         }
  61.     }
  62.  
  63.     void FitCameraToObjectBounds() {
  64.  
  65.         float maxSide = _objectBounds.size.z > _objectBounds.size.y ? _objectBounds.size.z : _objectBounds.size.y;
  66.         _iconCam.orthographicSize = maxSide * 0.6f;     // 0.5 will fit the icon perfectly, 0.6 to have some free space around the object
  67.     }
  68.  
  69.     IEnumerator GetSpriteFromTexture() {
  70.         yield return new WaitForEndOfFrame();
  71.  
  72.         Texture2D texture = RenderTextureToTexture2D(_renderTex);
  73.         Sprite sprite = Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  74.  
  75.         _outputImage.sprite = sprite;
  76.  
  77.         if (_destroyCopy)
  78.             Destroy(_copyObject);
  79.     }
  80.  
  81.     void SetLayerToChildren(GameObject targetObj, int layer) {
  82.         targetObj.layer = layer;
  83.  
  84.         foreach(Transform child in targetObj.transform) {
  85.             SetLayerToChildren(child.gameObject, layer);
  86.         }
  87.     }
  88.  
  89.     Texture2D RenderTextureToTexture2D(RenderTexture renderTex) {
  90.         Texture2D destination = new Texture2D(renderTex.width, renderTex.height, TextureFormat.RGBA32, false);
  91.         destination.Apply();
  92.         Graphics.CopyTexture(renderTex, destination);
  93.         return destination;
  94.     }
  95. }
  96.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement