using UnityEngine;

namespace Jovian.Utilities {
[RequireComponent(typeof(Canvas))]
    public class CanvasAutoAssignWorldCamera : MonoBehaviour {
        public Canvas canvas;
        public bool autoDisableOnceCameraFound = false;

        #if UNITY_EDITOR
        public void Reset() {
            SerializedObjectUtility.SaveObjectProperties(this, nameof(canvas), GetComponent<Canvas>());
        }
        #endif

        private void Update() {
            if(canvas && !canvas.worldCamera) {
                canvas.worldCamera = CachedMainCamera.MainCamera;
                if(canvas.worldCamera && autoDisableOnceCameraFound) {
                    enabled = false;
                }
            }
        }
    }
}