/// <summary> /// Instantiates a UITracker with and automatically sets it up. /// </summary> /// <param name="source">The Tracker to instantiate (normally a prefab).</param> /// <param name="trackedObject">The object that this UITracker will track.</param> /// <param name="optionalCanvas">An optional canvas to be used. If no canvas is provided, a new one with default settings will be created for this tracker.</param> /// <returns></returns> public static UITracker InstantiateTracker(UITracker source, GameObject trackedObject, Canvas canvas = null) { bool mustCleanupCanvas = false; if (!canvas) { //Create a canvas GameObject canvasGO = new GameObject($"{trackedObject.name}'s UI Tracking Canvas"); canvasGO.layer = LayerMask.NameToLayer("UI"); canvas = canvasGO.AddComponent <Canvas>(); mustCleanupCanvas = true; } canvas.renderMode = RenderMode.ScreenSpaceOverlay; //TODO: Add a CanvasScaler. Where do we get the reference resolution from? Does it even matter? /* * CanvasScaler canvasScaler = canvasGO.AddComponent<CanvasScaler>(); * canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; * canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; * canvasScaler.matchWidthOrHeight = 0.5f; * canvasScaler.referenceResolution = canvasReferenceResolution; */ UITracker newTracker = Instantiate(source, canvas.transform); newTracker.TrackedObject = trackedObject; newTracker.mustCleanupCanvasOnDestroy = mustCleanupCanvas; return(newTracker); }
void Start() { Debug.Assert(uiTrackerPrefab, "UITracked must have a uiTrackerPrefab to instantiate.", this); Tracker = UITracker.InstantiateTracker(uiTrackerPrefab, gameObject, GetCanvas()); }
private void Awake() { Tracker = GetComponent <UITracker>(); }