예제 #1
0
 public void Cleanup()
 {
     if (sprite != null)
     {
         if (Application.isPlaying)
         {
             Destroy(gameObject);
         }
         else
         {
             MadUndo.DestroyObjectImmediate(gameObject);
         }
     }
 }
예제 #2
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);
            }
        }
    }