Esempio n. 1
0
    private void Finish()
    {
        Anim(1);

        if (sendMessageOnFinish)
        {
            GameObject receiver = gameObject;
            if (messageReceiver != null)
            {
                receiver = messageReceiver;
            }

            receiver.SendMessage(messageName);
        }

        if (playAnimationOnFinish && !string.IsNullOrEmpty(playAnimationOnFinishName))
        {
            MadAnim.PlayAnimation(gameObject, playAnimationOnFinishName, playAnimationOnFinishFromTheBeginning);
        }

        if (destroyObjectOnFinish)
        {
            MadGameObject.SafeDestroy(gameObject);
        }
        isPlaying = false;

        if (!string.IsNullOrEmpty(animationQueue))
        {
            PlayAnimationNow(gameObject, animationQueue);
        }
    }
Esempio n. 2
0
    // ===========================================================
    // Static Methods
    // ===========================================================
    
    public static T CreateChild<T>(Transform parent, string name, bool disabled = false) where T : Component {
        GameObject go = null;
    
        go = new GameObject(name);
        if (disabled) {
            MadGameObject.SetActive(go, false);
        }

        go.transform.parent = parent;
        
        go.transform.localRotation = Quaternion.identity;
        go.transform.localScale = Vector3.one;
        go.transform.localPosition = Vector3.zero;
        
        T component = go.GetComponent<T>();
        if (component == null) {
            component = go.AddComponent<T>();
        }

        if (registerUndo) {
            MadUndo.RegisterCreatedObjectUndo(go, "Created " + name);
        }
        
        return component;
    }
Esempio n. 3
0
 void OnDisable()
 {
     if (drawCall != null)
     {
         MadGameObject.SetActive(drawCall.gameObject, false);
     }
 }
Esempio n. 4
0
    void CleanUp(bool forceDelete)
    {
        int levelCount = configuration.LevelCount(MadLevel.Type.Level);
        var children   = MadTransform.FindChildren <MadLevelIcon>(transform, (icon) => icon.hasLevelConfiguration);

        if (forceDelete)
        {
            foreach (var child in children)
            {
                MadGameObject.SafeDestroy(child.gameObject);
            }
        }
        else
        {
            var sorted = from c in children orderby c.levelIndex ascending select c;

            foreach (MadLevelIcon levelIcon in sorted)
            {
                if (levelIcon.levelIndex >= levelCount)
                {
                    // disable leftovers
                    MadGameObject.SetActive(levelIcon.gameObject, false);
                }
            }
        }
    }
    private void StartLoaded()
    {
        state = State.Loaded;

        onLoadedMessage.Execute();

        if (testMode && TestLevelDefined())
        {
            TestLoadLevel();
        }

        if (whenLevelLoaded != WhenLevelLoaded.ShowImmediately)
        {
            for (int i = 0; i < changeDisable.Count; i++)
            {
                var toDisable = changeDisable[i];
                if (toDisable != null)
                {
                    MadGameObject.SetActive(toDisable.gameObject, false);
                }
            }

            for (int i = 0; i < changeEnable.Count; i++)
            {
                var toEnable = changeEnable[i];
                if (toEnable != null)
                {
                    MadGameObject.SetActive(toEnable.gameObject, true);
                }
            }
        }
    }
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    public override MadLevelIcon GetIcon(string levelName)
    {
        MadDebug.Assert(!string.IsNullOrEmpty(levelName), "null or empty level name");
        return(MadTransform.FindChild <MadLevelIcon>(draggable.transform, (icon) => {
            return MadGameObject.IsActive(icon.gameObject) && icon.level.name == levelName;
        }, 0));
    }
Esempio n. 7
0
    /// <summary>
    /// Gets all active icons available in the layout.
    /// </summary>
    /// <returns></returns>
    public MadLevelIcon[] GetAllIcons()
    {
        // find under the page
        var icons = MadTransform.FindChildren <MadLevelIcon>(
            transform, (ic) => MadGameObject.IsActive(ic.gameObject), 3);

        return(icons.ToArray());
    }
Esempio n. 8
0
 void OnDisable()
 {
     for (int i = 0; i < drawCalls.Count; i++)
     {
         var drawCall = drawCalls[i];
         MadGameObject.SetActive(drawCall.gameObject, false);
     }
 }
    void RebuildClean()
    {
        var generated = MadTransform.FindChildren <MadSprite>(transform, (sprite) => sprite.name.StartsWith("generated_"));

        foreach (var obj in generated)
        {
            MadGameObject.SafeDestroy(obj.gameObject);
        }
    }
Esempio n. 10
0
    static void SlideSetActive(MadSprite icon, bool act)
    {
        bool currentlyActive = MadGameObject.IsActive(icon.gameObject);

        if (currentlyActive != act)
        {
            MadGameObject.SetActive(icon.gameObject, act);
        }
    }
Esempio n. 11
0
    protected override void Update()
    {
        base.Update();

        if (justEnabled)
        {
            if (Application.isPlaying)
            {
                if (isTemplate)
                {
                    MadGameObject.SetActive(gameObject, false);
                }

                // completed property object is optional
                // if it's not present, check the completed property manually
                if (completedProperty == null)
                {
                    if (level != null)
                    {
                        completed = MadLevelProfile.IsCompleted(level.name);
                    }
                }

                onMouseUp += (sprite) => Activate();
                onTap     += (sprite) => Activate();

                if (!isTemplate)
                {
                    if (!canFocusIfLocked && locked)
                    {
                        var sprite = GetComponent <MadSprite>();
                        sprite.eventFlags = sprite.eventFlags & ~MadSprite.EventFlags.Focus;
                    }
                }
            }

            // init child objects visibility
            if (level != null)
            {
                ChangeState(showWhenLevelLocked, locked);
                ChangeState(showWhenLevelUnlocked, !locked);
                ChangeState(showWhenLevelCompleted, completed);
                ChangeState(showWhenLevelNotCompleted, !completed);

                // if this level is mark as unlocked, make sure that it has a profile entry
                // this is a workaround for situation when level is unlocked, but then level order is changed
                if (!locked && Application.isPlaying)
                {
                    MadLevelProfile.SetLocked(level.name, false);
                }
            }

            justEnabled = false;
        }
    }
Esempio n. 12
0
    public void Destroy()
    {
        MadGameObject.SetActive(gameObject, false);

#if UNITY_EDITOR
        EditorApplication.delayCall += () => {
            DestroyImmediate(gameObject);
        };
#else
        MadGameObject.SafeDestroy(gameObject);
#endif
    }
Esempio n. 13
0
    // deactivates all other icons under the draggable than given
    private void DeactivateAllOther(HashSet <MadLevelIcon> activeIcons)
    {
        var allIcons = MadTransform.FindChildren <MadLevelIcon>(draggable.transform);

        foreach (var icon in allIcons)
        {
            if (!activeIcons.Contains(icon))
            {
                MadGameObject.SetActive(icon.gameObject, false);
            }
        }
    }
Esempio n. 14
0
    static void SlideSetActive(MadSprite icon, bool act)
    {
        if (!MadGameObject.IsActive(icon.gameObject))
        {
            MadGameObject.SetActive(icon.gameObject, true);
        }

        bool currentlyActive = icon.visible;

        if (currentlyActive != act)
        {
            icon.visible = act;
        }
    }
Esempio n. 15
0
 // if a scene has only one Main Camera object, then init tool proposes to remove it
 private static void CheckEmptyScene()
 {
     if (SceneHasOnlyMainCamera())
     {
         bool remove = EditorUtility.DisplayDialog(
             "Remove Main Camera?",
             "Your scene seems to have only the Main Camera object. If you're not planning to use it, it's recommended to remove it.",
             "Remove", "Leave");
         if (remove)
         {
             var mainCamera = GameObject.Find("/Main Camera");
             MadGameObject.SafeDestroy(mainCamera);
         }
     }
 }
Esempio n. 16
0
    void InitTemplates(MadRootNode root, out MadLevelIcon icon, out MadSprite slideLeftSprite,
                       out MadSprite slideRightSprite)
    {
        var panel     = MadTransform.FindChild <MadPanel>(root.transform);
        var templates = MadTransform.CreateChild(panel.transform, "Templates");

        GameObject iconPrefab       = (GameObject)AssetDatabase.LoadAssetAtPath(IconPrefab, typeof(GameObject));
        GameObject slideLeftPrefab  = (GameObject)AssetDatabase.LoadAssetAtPath(SlideLeftPrefab, typeof(GameObject));
        GameObject slideRightPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(SlideRightPrefab, typeof(GameObject));

        if (MadGameObject.AnyNull(iconPrefab, slideLeftPrefab, slideRightPrefab))
        {
            Debug.LogWarning("I cannot find all needed prefabs to create example templates. Have you moved Mad Level "
                             + "Manager directory to other than default place?");
        }

        if (iconPrefab != null)
        {
            var obj = MadTransform.CreateChild(templates.transform, "icon", iconPrefab);
            icon = obj.GetComponent <MadLevelIcon>();
        }
        else
        {
            icon = null;
        }

        if (slideLeftPrefab != null)
        {
            var slide = MadTransform.CreateChild(templates.transform, "slide left", slideLeftPrefab);
            slideLeftSprite = slide.GetComponent <MadSprite>();
        }
        else
        {
            slideLeftSprite = null;
        }

        if (slideRightPrefab != null)
        {
            var slide = MadTransform.CreateChild(templates.transform, "slide right", slideRightPrefab);
            slideRightSprite = slide.GetComponent <MadSprite>();
        }
        else
        {
            slideRightSprite = null;
        }

        MadGameObject.SetActive(templates, false);
    }
#pragma warning disable 429, 162
    private void Unity5Check()
    {
        // generated check
        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
        if ("MadLevelManager" != ("_NAME" + "SPACE_") && "MadLevelManager" != "MadLevelManager")
        {
            return;
        }

        if (Application.isPlaying)
        {
            return;
        }

#if UNITY_EDITOR
        int majorVersion;
        if (int.TryParse(Application.unityVersion.Split('.')[0], out majorVersion))
        {
            if (majorVersion >= 5 && renderMode == RenderMode.Legacy)
            {
                var nonAtlasSprite = MadTransform.FindChild <MadSprite>(transform,
                                                                        (s) => {
                    if (s is MadText)
                    {
                        return(((MadText)s).atlas == null);
                    }
                    else
                    {
                        return(s.inputType == MadSprite.InputType.SingleTexture && MadGameObject.IsActive(s.gameObject));
                    }
                });
                if (nonAtlasSprite != null)
                {
                    bool sw = EditorUtility.DisplayDialog(
                        "Unity 5 Compatibility",
                        "With Unity 5 is recommended to switch to Depth-Based render mode or Legacy mode with atlases only. " +
                        "Please see Unity 5 Compatibility docmentation page for more information. " +
                        "Do you want to switch to Depth-Based mode?\n\n",
                        "Yes (Recommended)", "No");
                    if (sw)
                    {
                        renderMode = RenderMode.DepthBased;
                    }
                }
            }
        }
#endif
    }
Esempio n. 18
0
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================

    void OnEnable()
    {
        if (drawCall == null)
        {
            drawCall = MadDrawCall.Create();
            drawCall.gameObject.layer = gameObject.layer;

            MadTransform.SetLocalScale(drawCall.transform, transform.lossyScale);
        }

        panel = GetComponent <MadPanel>();

        if (drawCall != null)
        {
            MadGameObject.SetActive(drawCall.gameObject, true);
        }
    }
Esempio n. 19
0
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================

    void OnEnable()
    {
        panel = GetComponent <MadPanel>();

        // enable existing draw calls
        for (int i = 0; i < drawCalls.Count; i++)
        {
            var drawCall = drawCalls[i];
            if (drawCall == null)
            {
                drawCalls.RemoveAt(i);
                i--;
            }
            else
            {
                MadGameObject.SetActive(drawCall.gameObject, true);
            }
        }
    }
Esempio n. 20
0
    void OnEnable()
    {
        panels.Add(this);

        materialStore = GetComponent <MadMaterialStore>();

        var meshRenderer = gameObject.GetComponent <MeshRenderer>();

        if (meshRenderer != null)
        {
            MadGameObject.SafeDestroy(meshRenderer);
        }

        var meshFilter = gameObject.GetComponent <MeshFilter>();

        if (meshFilter != null)
        {
            MadGameObject.SafeDestroy(meshFilter);
        }
    }
Esempio n. 21
0
    private MadDrawCall NextDrawCall()
    {
        MadDrawCall drawCall;

        if (nextDrawCall >= drawCalls.Count)
        {
            // create new draw call
            drawCall = MadDrawCall.Create();
            drawCall.gameObject.layer = gameObject.layer;

            MadTransform.SetLocalScale(drawCall.transform, transform.lossyScale);
            drawCalls.Add(drawCall);
            nextDrawCall++;
        }
        else
        {
            drawCall = drawCalls[nextDrawCall++];
            MadGameObject.SetActive(drawCall.gameObject, true);
        }

        return(drawCall);
    }
Esempio n. 22
0
    // ===========================================================
    // Methods
    // ===========================================================

    void FirstUpdate()
    {
        var layout = MadLevelLayout.current;

        if (layout.twoStepActivationType == MadLevelAbstractLayout.TwoStepActivationType.Disabled)
        {
            Debug.LogError("Input controller cannot work when two step activation is disabled! Please enable it!", this);
            MadGameObject.SetActive(gameObject, false);
            return;
        }

        if (layout.twoStepActivationType == MadLevelAbstractLayout.TwoStepActivationType.OnlyOnMobiles && !onlyOnMobiles)
        {
            Debug.LogError("Two step activation is set to work on mobiles, but input controler is not!", this);
            MadGameObject.SetActive(gameObject, false);
            return;
        }

        isMobile = SystemInfo.deviceType == DeviceType.Handheld;

        DoActivateOnStart();
    }
Esempio n. 23
0
    private void CleanUp(bool forceDelete)
    {
        int levelCount = currentConfiguration.LevelCount(MadLevel.Type.Level, configurationGroup);
        var children   = MadTransform.FindChildren <MadLevelIcon>(transform, (icon) => icon.hasLevelConfiguration);

        if (forceDelete)
        {
            foreach (var child in children)
            {
                DestroyImmediate(child.gameObject);
            }

            // remove all pages
            var pages = MadTransform.FindChildren <Transform>(transform, (t) => t.name.StartsWith("Page "));
            foreach (var page in pages)
            {
                DestroyImmediate(page.gameObject);
            }
        }
        else
        {
            var sorted = from c in children orderby c.levelIndex ascending select c;

            foreach (MadLevelIcon levelIcon in sorted)
            {
                if (levelIcon.levelIndex >= levelCount)
                {
                    // disable leftovers
                    MadGameObject.SetActive(levelIcon.gameObject, false);
                }
            }
        }

        // remove slides
        ClearSlide("SlideLeftAnchor");
        ClearSlide("SlideRightAnchor");
    }
Esempio n. 24
0
    private static void Initialize() {
        var guiRootPrefab = AssetDatabase.LoadAssetAtPath(
            AssetDatabase.GUIDToAssetPath(GUIRootGUID), typeof(GameObject)) as GameObject;
        var loadingScriptPrefab = AssetDatabase.LoadAssetAtPath(
            AssetDatabase.GUIDToAssetPath(LoadingScriptGUID), typeof (GameObject)) as GameObject;

        var guiRootInstance = Object.Instantiate(guiRootPrefab) as GameObject;
        var loadingScriptInstance = Object.Instantiate(loadingScriptPrefab) as GameObject;

        guiRootInstance.name = guiRootPrefab.name;
        loadingScriptInstance.name = loadingScriptPrefab.name;

        PrefabUtility.DisconnectPrefabInstance(guiRootInstance);
        PrefabUtility.DisconnectPrefabInstance(loadingScriptInstance);

        var loadingBar = MadTransform.FindChild<MadSprite>(guiRootInstance.transform, sprite => sprite.name == "bar").gameObject;
        var loadingText = MadTransform.FindChild<MadText>(guiRootInstance.transform, text => text.name == "loading text").gameObject;
        var loadedText = MadTransform.FindChild<MadText>(guiRootInstance.transform, text => text.name == "loaded text").gameObject;
        var pressAnywhereText = MadTransform.FindChild<MadText>(guiRootInstance.transform, text => text.name == "press anywhere text").gameObject;

        var loadingScreen = loadingScriptInstance.GetComponent<LoadingScreen>();

        loadingScreen.loadingBar = loadingBar;

        loadingScreen.changeDisable.Clear();
        loadingScreen.changeDisable.Add(loadingText);

        loadingScreen.changeEnable.Clear();
        loadingScreen.changeEnable.Add(loadedText);
        loadingScreen.changeEnable.Add(pressAnywhereText);

        MadGameObject.SetActive(loadedText, false);
        MadGameObject.SetActive(pressAnywhereText, false);

        new GameObject("_mlm_ignore");
    }
Esempio n. 25
0
    private static bool SceneHasOnlyMainCamera()
    {
        bool hasOtherObjects = false;
        bool hasMainCamera   = false;

        var allObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];

        foreach (var obj in allObjects)
        {
            if (MadGameObject.IsActive(obj))
            {
                if (obj.name == "Main Camera")
                {
                    hasMainCamera = true;
                }
                else
                {
                    hasOtherObjects = true;
                }
            }
        }

        return(hasMainCamera && !hasOtherObjects);
    }
Esempio n. 26
0
    // if a scene has only one Main Camera object, then init tool proposes to remove it
    private static void CheckEmptyScene()
    {
        if (SceneIsNewlyCreated())
        {
            bool remove = EditorUtility.DisplayDialog(
                "Remove Unused Objects?",
                "It seems that this is a newly created scene. Level select screen do not need the Main Camera or Directional Light objects. If you're not planning to use it, it's recommended to remove it.",
                "Remove", "Leave");
            if (remove)
            {
                var mainCamera = GameObject.Find("/Main Camera");
                if (mainCamera != null)
                {
                    MadGameObject.SafeDestroy(mainCamera);
                }

                var light = GameObject.Find("/Directional Light");
                if (light != null)
                {
                    MadGameObject.SafeDestroy(light);
                }
            }
        }
    }
Esempio n. 27
0
    void Build()
    {
        List <MadLevelIcon> allIcons = new List <MadLevelIcon>();

        allIcons.AddRange(MadTransform.FindChildren <MadLevelIcon>(transform));

        int     levelCount    = currentConfiguration.LevelCount(MadLevel.Type.Level, configurationGroup);
        Vector2 currentOffset = Vector2.zero;

        MadLevelIcon previousIcon = null;

        // find out min and max depth
        int min, max;

        iconTemplate.MinMaxDepthRecursively(out min, out max);
        const string name = "level {0:D3}";

        HashSet <MadLevelIcon> activeIcons = new HashSet <MadLevelIcon>();

        for (int levelIndex = 0; levelIndex < levelCount; ++levelIndex)
        {
            MadLevelIcon levelIcon = MadTransform.FindChild <MadLevelIcon>(
                draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
            bool newInstance = levelIcon == null;

            // create new icon instance if it's not exists
            if (newInstance)
            {
                levelIcon = CreateIcon(draggable.transform, string.Format(name, levelIndex + 1), iconTemplate);

                // position & scale
                levelIcon.pivotPoint           = MadSprite.PivotPoint.Center;
                levelIcon.transform.localScale = Vector3.one;

                do
                {
                    levelIcon.transform.localPosition = currentOffset;
                    currentOffset += offset;
                } while (Collides(levelIcon, allIcons));

                allIcons.Add(levelIcon);
            }

            // make it active if deactivated
            if (!MadGameObject.IsActive(levelIcon.gameObject))
            {
                MadGameObject.SetActive(levelIcon.gameObject, true);
            }

            // setup level properties
            levelIcon.levelGroup            = configurationGroup;
            levelIcon.levelIndex            = levelIndex;
            levelIcon.configuration         = currentConfiguration;
            levelIcon.hasLevelConfiguration = true;

            // set level number if exists
            if (levelIcon.levelNumber != null)
            {
                levelIcon.levelNumber.text = GetEnumerationValue(levelIndex);
            }

            // level unlock if set
            if (previousIcon != null)
            {
                if (newInstance)
                {
                    previousIcon.unlockOnComplete.Add(levelIcon);
                }
            }
            else
            {
                levelIcon.locked = false;
            }

            if (!Application.isPlaying || !MadLevelProfile.IsLockedSet(levelIcon.level.name))
            {
                levelIcon.locked = levelIcon.level.lockedByDefault;
            }

            previousIcon = levelIcon;

            activeIcons.Add(levelIcon);
        }

        BuildBackgroundTexture();

        DeactivateAllOther(activeIcons);
    }
Esempio n. 28
0
    /// <summary>
    /// Will replace all icons in the layout with selected icon. Position, scale and rotation will be preserved.
    /// This method is meant for editor-use only.
    /// </summary>
    /// <param name="newIcon"></param>
    public void ReplaceIcons(GameObject newIcon)
    {
        if (Application.isPlaying)
        {
            Debug.LogError("This method can be called only from the editor");
            return;
        }

        MadUndo.LegacyRegisterSceneUndo("Replaced Icons");

        var icons       = MadTransform.FindChildren <MadLevelIcon>(draggable.transform);
        var activeIcons = from i in icons where MadGameObject.IsActive(i.gameObject) select i;

        // keep track of unlock on complete settings
        Dictionary <int, List <int> > unlockOnCompleteDict = new Dictionary <int, List <int> >();
        List <MadLevelIcon>           createdIcons         = new List <MadLevelIcon>();

        foreach (var icon in activeIcons)
        {
            var position      = icon.transform.position;
            var rotation      = icon.transform.rotation;
            var localScale    = icon.transform.localScale;
            var name          = icon.name;
            var baseDepth     = icon.guiDepth;
            var levelIndex    = icon.levelIndex;
            var configuration = icon.configuration;

            // build unlock on complete dict
            List <int> toUnlockList = new List <int>();
            foreach (var unlock in icon.unlockOnComplete)
            {
                toUnlockList.Add(unlock.levelIndex);
            }
            unlockOnCompleteDict[icon.levelIndex] = toUnlockList;


            MadUndo.DestroyObjectImmediate(icon.gameObject);

            var nIcon = CreateIcon(draggable.transform, name, iconTemplate);
            nIcon.transform.position    = position;
            nIcon.transform.rotation    = rotation;
            nIcon.transform.localScale  = localScale;
            nIcon.guiDepth              = baseDepth;
            nIcon.levelIndex            = levelIndex;
            nIcon.configuration         = configuration;
            nIcon.hasLevelConfiguration = true;

            createdIcons.Add(nIcon);


            var childSprites = MadTransform.FindChildren <MadSprite>(nIcon.transform);
            foreach (var cs in childSprites)
            {
                cs.guiDepth += baseDepth;
            }

            MadUndo.RegisterCreatedObjectUndo(nIcon.gameObject, "Replaced Icons");
        }

        icons = MadTransform.FindChildren <MadLevelIcon>(draggable.transform);

        // apply unlock on complete list
        foreach (var icon in createdIcons)
        {
            List <int> unlockList = unlockOnCompleteDict[icon.levelIndex];
            foreach (var unlockLevelIndex in unlockList)
            {
                var          query        = from i in icons where i.levelIndex == unlockLevelIndex select i;
                MadLevelIcon iconToUnlock = query.First();
                icon.unlockOnComplete.Add(iconToUnlock);
            }
        }
    }
Esempio n. 29
0
    // builds level icons that are absent now
    void Build()
    {
        // create or get a draggable
        draggable = MadTransform.GetOrCreateChild <MadDragStopDraggable>(transform, "Draggable");

        draggable.dragStopCallback = (index) => {
            pageCurrentIndex = index;
        };

        float startX = -pixelsWidth / 2;
        float startY = pixelsHeight / 2;

        float dx = pixelsWidth / (gridWidth + 1);
        float dy = -pixelsHeight / (gridHeight + 1);

        int levelNumber = 1;

        MadLevelIcon previousIcon = null;

        int levelCount = configuration.LevelCount(MadLevel.Type.Level);
        int levelIndex = 0;

        int pageIndex = 0;

        while (levelIndex < levelCount)
        {
            Transform page = MadTransform.FindChild <Transform>(draggable.transform, (t) => t.name == "Page " + (pageIndex + 1));
            bool      createPageInstance = page == null;

            if (createPageInstance)
            {
                page = MadTransform.CreateChild <Transform>(draggable.transform, "Page " + (pageIndex + 1));
            }

            if (createPageInstance || generate)
            {
                page.localPosition = new Vector3(pagesXOffset * pageIndex, 0, 0);
            }


            for (int y = 1; y <= gridHeight && levelIndex < levelCount; ++y)
            {
                for (int x = 1; x <= gridWidth && levelIndex < levelCount; ++x, levelIndex++)
                {
                    // update info: in previous versions page was nested directly under draggable
                    // now they should be placed inside "Page X" transforms
                    MadLevelIcon levelIcon;

                    // look directly under Draggable
                    levelIcon = MadTransform.FindChild <MadLevelIcon>(
                        draggable.transform, (ic) => ic.levelIndex == levelIndex, 0);
                    if (levelIcon != null)
                    {
                        // move to page
                        levelIcon.transform.parent = page;
                    }
                    else
                    {
                        // find under the page
                        levelIcon = MadTransform.FindChild <MadLevelIcon>(
                            page.transform, (ic) => ic.levelIndex == levelIndex, 0);
                    }

                    bool createNewInstance = levelIcon == null;
                    var  level             = configuration.GetLevel(MadLevel.Type.Level, levelIndex);

                    if (createNewInstance)
                    {
                        levelIcon = MadTransform.CreateChild(
                            page.transform, level.name, iconTemplate);
                    }
                    else
                    {
                        levelIcon.name = level.name;
                    }

                    levelIcon.gameObject.hideFlags = generate ? HideFlags.HideInHierarchy : 0;

                    if (!MadGameObject.IsActive(levelIcon.gameObject))
                    {
                        MadGameObject.SetActive(levelIcon.gameObject, true);
                    }

                    levelIcon.levelIndex            = levelIndex;
                    levelIcon.configuration         = configuration;
                    levelIcon.hasLevelConfiguration = true;

                    if (generate || createNewInstance)
                    {
                        levelIcon.pivotPoint = MadSprite.PivotPoint.Center;

                        levelIcon.transform.localPosition =
                            new Vector3(startX + dx * x + iconOffset.x, startY + dy * y + iconOffset.y, 0);

                        levelIcon.transform.localScale = new Vector3(iconScale.x, iconScale.y, 1);

                        if (levelIcon.levelNumber != null)
                        {
                            levelIcon.levelNumber.text = levelNumber.ToString();
                        }
                    }

                    if (previousIcon != null)
                    {
                        if (createNewInstance)
                        {
                            previousIcon.unlockOnComplete.Add(levelIcon);
                        }
                    }
                    else
                    {
                        if (generate)
                        {
                            levelIcon.locked = false;
                        }
                    }

                    previousIcon = levelIcon;
                    levelNumber++;
                }
            }

            pageIndex++;
        }

        BuildSlideIcons();
        BuildDragging(draggable, (int)Mathf.Ceil((float)levelCount / (gridWidth * gridHeight)));

        // enable/disable selection based on hide settings
        var sprites = GetComponentsInChildren <MadSprite>();

        foreach (var sprite in sprites)
        {
            sprite.editorSelectable = !generate;
        }
    }
Esempio n. 30
0
    void InitTemplates(MadRootNode root, out MadLevelIcon icon, out MadSprite slideLeftSprite,
                       out MadSprite slideRightSprite)
    {
        var panel     = MadTransform.FindChild <MadPanel>(root.transform);
        var templates = MadTransform.CreateChild(panel.transform, "Templates");

        GameObject iconPrefab       = MadAssets.TryLoadGameObject(IconPrefabGUID);
        GameObject slideLeftPrefab  = MadAssets.TryLoadGameObject(SlideLeftPrefabGUID);
        GameObject slideRightPrefab = MadAssets.TryLoadGameObject(SlideRightPrefabGUID);

        if (MadGameObject.AnyNull(iconPrefab, slideLeftPrefab, slideRightPrefab))
        {
            Debug.LogWarning("I cannot find all needed prefabs to create example templates. Have you removed Mad Level "
                             + "Manager example prefabs?");
        }

        if (iconPrefab != null)
        {
            var obj = PrefabUtility.InstantiatePrefab(iconPrefab) as GameObject;
            obj.transform.parent = templates.transform;
            obj.name             = "icon";

            obj.transform.localScale    = Vector3.one;
            obj.transform.localPosition = new Vector2(-400, 150);
            icon = obj.GetComponent <MadLevelIcon>();
        }
        else
        {
            icon = null;
        }

        if (slideLeftPrefab != null)
        {
            var slide = PrefabUtility.InstantiatePrefab(slideLeftPrefab) as GameObject;
            slide.transform.parent = templates.transform;
            slide.name             = "slide left";

            slide.transform.localScale    = Vector3.one;
            slide.transform.localPosition = new Vector2(-400, 0);
            slideLeftSprite = slide.GetComponent <MadSprite>();
        }
        else
        {
            slideLeftSprite = null;
        }

        if (slideRightPrefab != null)
        {
            var slide = PrefabUtility.InstantiatePrefab(slideRightPrefab) as GameObject;
            slide.transform.parent = templates.transform;
            slide.name             = "slide right";

            slide.transform.localScale    = Vector3.one;
            slide.transform.localPosition = new Vector2(-400, -150);
            slideRightSprite = slide.GetComponent <MadSprite>();
        }
        else
        {
            slideRightSprite = null;
        }

        MadGameObject.SetActive(templates, false);
    }