Exemple #1
0
        static void Create()
        {
            GameObject    go = CreateUIElementRoot("Minimap", new Vector2(250, 250));
            RectTransform rt = go.GetComponent <RectTransform>();

            rt.anchorMin        = Vector2.one;
            rt.anchorMax        = Vector2.one;
            rt.pivot            = Vector2.one;
            rt.anchoredPosition = new Vector2(rt.anchoredPosition.x - 10, rt.anchoredPosition.y - 10);

            Map map = go.AddComponent <Map>();

            map.material = NJGEditorTools.GetMaterial(map, true);

            Image img = map.iconRoot.gameObject.GetComponent <Image>();

            img.sprite = NJGEditorTools.GetDefaultMask(map == Map.miniMap);

            GameObject bg = new GameObject("Background");

            bg.AddComponent <Image>().color = new Color(80f / 255f, 80f / 255f, 80f / 255f, 1f);
            bg.transform.SetParent(go.transform, false);
            bg.transform.SetAsFirstSibling();
            rt           = bg.GetComponent <RectTransform>();
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.sizeDelta = Vector2.zero;

            Canvas canvas = Selection.activeGameObject ? Selection.activeGameObject.GetComponent <Canvas>() : null;

            if (!canvas)
            {
                canvas = Object.FindObjectOfType <Canvas>();
            }

            if (!canvas)
            {
                canvas = new GameObject("Canvas", typeof(Canvas), typeof(GraphicRaycaster), typeof(CanvasScaler)).GetComponent <Canvas>();
                canvas.gameObject.layer = LayerMask.NameToLayer("UI");
                canvas.renderMode       = RenderMode.ScreenSpaceCamera;

                Undo.RegisterCreatedObjectUndo(canvas, "Create " + canvas.name);
            }

            if (canvas.worldCamera == null)
            {
                Camera cam = new GameObject("UICamera", typeof(Camera)).GetComponent <Camera>();
                cam.cullingMask    = 1 << LayerMask.NameToLayer("UI");
                cam.clearFlags     = CameraClearFlags.Depth;
                cam.orthographic   = true;
                canvas.worldCamera = cam;
            }

            if (canvas)
            {
                go.transform.SetParent(canvas.transform, false);
            }

            EventSystem esys = Object.FindObjectOfType <EventSystem>();

            if (esys == null)
            {
                var eventSystem = new GameObject("EventSystem");
                GameObjectUtility.SetParentAndAlign(eventSystem, null);
                esys = eventSystem.AddComponent <EventSystem>();
                eventSystem.AddComponent <StandaloneInputModule>();
                //eventSystem.AddComponent<TouchInputModule>();

                Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name);
            }

            Button zoomIn = CreateButton("Button - ZoomIn", "+", new Vector2(30, 30));

            zoomIn.gameObject.AddComponent <ButtonZoom>().zoomIn = true;
            zoomIn.gameObject.GetComponent <ButtonZoom>().map    = map;
            zoomIn.transform.SetParent(go.transform, false);

            rt           = zoomIn.gameObject.GetComponent <RectTransform>();
            rt.pivot     = Vector2.one;
            rt.anchorMin = Vector2.one;
            rt.anchorMax = Vector2.one;

            Button zoomOut = CreateButton("Button - ZoomOut", "-", new Vector2(30, 30));

            zoomOut.gameObject.AddComponent <ButtonZoom>().zoomIn = false;
            zoomOut.gameObject.GetComponent <ButtonZoom>().map    = map;
            zoomOut.transform.SetParent(go.transform, false);

            rt                  = zoomOut.gameObject.GetComponent <RectTransform>();
            rt.pivot            = Vector2.one;
            rt.anchorMin        = Vector2.one;
            rt.anchorMax        = Vector2.one;
            rt.anchoredPosition = new Vector2(rt.anchoredPosition.x, rt.anchoredPosition.y - 32);

            Button lockMap = CreateButton("Button - Lock", "L", new Vector2(30, 30));

            lockMap.gameObject.AddComponent <ButtonLockMap>().map = map;
            lockMap.transform.SetParent(go.transform, false);

            rt           = lockMap.gameObject.GetComponent <RectTransform>();
            rt.pivot     = new Vector2(0, 1);
            rt.anchorMin = new Vector2(0, 1);
            rt.anchorMax = new Vector2(0, 1);

            Text text = new GameObject("WorldName", typeof(Text), typeof(WorldName)).GetComponent <Text>();

            text.transform.SetParent(go.transform, false);
            SetDefaultTextValues(text);
            text.text = "World Name";
            text.gameObject.AddComponent <Outline>();
            text.fontSize  = 18;
            text.alignment = TextAnchor.MiddleCenter;

            rt           = text.gameObject.GetComponent <RectTransform>();
            rt.pivot     = new Vector2(0.5f, 1);
            rt.anchorMin = new Vector2(0, 1);
            rt.anchorMax = new Vector2(1, 1);
            rt.sizeDelta = new Vector2(-80f, 60f);

            text = new GameObject("MapCoords", typeof(Text), typeof(MapCoords)).GetComponent <Text>();
            text.transform.SetParent(go.transform, false);
            SetDefaultTextValues(text);
            text.text = "x:0 y:0";
            text.gameObject.AddComponent <Outline>();
            text.fontSize  = 14;
            text.alignment = TextAnchor.LowerCenter;

            rt                  = text.gameObject.GetComponent <RectTransform>();
            rt.pivot            = new Vector2(0.5f, 1);
            rt.anchorMin        = new Vector2(0.5f, 0);
            rt.anchorMax        = new Vector2(0.5f, 0);
            rt.sizeDelta        = new Vector2(100f, 20f);
            rt.anchoredPosition = new Vector2(0, 25);

            SetLayerRecursively(go, LayerMask.NameToLayer("UI"));

            Undo.RegisterCreatedObjectUndo(go, "Create Minimap");

            Selection.activeGameObject = go;
        }