Inheritance: MonoBehaviour
Example #1
0
    // Use this for initialization
    public void SetupItems(List <Dictionary <string, string> > inItems)
    {
        Vector3     position    = gameObject.transform.position;
        int         index       = 0;
        const float itemHeight  = .15f;
        float       startY      = position.y;
        int         cnt         = inItems.Count;
        float       totalHeight = cnt * itemHeight;

        foreach (Dictionary <string, string> item in inItems)
        {
            position.y = startY + ((cnt - index) * itemHeight);

            // center vertically
            position.y -= totalHeight / 2;

            GameObject anItem = Instantiate(itemPrefab, position, Quaternion.identity) as GameObject;

            anItem.transform.parent = gameObject.transform;

            LevelMenuItem menuItem = anItem.GetComponent <LevelMenuItem>() as LevelMenuItem;

            menuItem.SetupItem(item, this);

            index++;
        }
    }
Example #2
0
 public void LoadLevel(LevelMenuItem levelMenuItem)
 {
     this.Hide();
     // @TODO( Load level.
     print("loading level: " + levelMenuItem.GetPath());
     PlayerPrefs.SetString(PlayerPrefKeys.LEVEL_CURRENT, levelMenuItem.GetPath());
     Application.LoadLevel("BaseScene");
 }
Example #3
0
    public void LoadItems()
    {
        foreach (Transform transform in levelSelectContent)
        {
            Destroy(transform.gameObject);
        }

        DirectoryInfo dir = new DirectoryInfo(Application.persistentDataPath + "/levels");

        dir.Create();
        FileInfo[] info = dir.GetFiles("*.dat");
        foreach (FileInfo f in info)
        {
            LevelMenuItem newItem = Instantiate(levelSelectPrefab, levelSelectContent).GetComponent <LevelMenuItem>();
            newItem.levelName = f.Name.Replace(".dat", "");
            newItem.UpdateUI();
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        //Determine level count from the number of levels from the level loader
        int levelCount = LevelLoading.GetLevelCount();

        int currentRow     = 0;
        int currentRowItem = 0; //Current Item in the row

        for (int i = 0; i < levelCount; ++i)
        {
            //Create button
            GameObject button = Instantiate(levelButtonPrefab);
            button.transform.SetParent(contentArea);
            button.transform.localScale = Vector3.one;

            //Calculate it's postion
            RectTransform buttonRect     = button.GetComponent <RectTransform>();
            Vector3       positionInGrid = new Vector3(currentRowItem * rowSpacing, currentRow * -columnsSpacing, 0f);
            Vector3       createPos      = startPoint + positionInGrid;
            buttonRect.localPosition = createPos;

            //Set Level ID of item
            LevelMenuItem levelData = button.GetComponent <LevelMenuItem>();
            if (levelData != null)
            {
                levelData.InitLevelButton(i + 1, this);
            }

            //Check if we shoud go to the next row
            currentRowItem++;
            if (currentRowItem % levelsPerRow == 0)
            {
                currentRow++;
                currentRowItem = 0;
            }
        }

        //Work out the required size of the content box so that the scroll rect
        //works properly
        float requiredContentSize = Mathf.Abs(startPoint.y) + ((currentRow + 1) * columnsSpacing);

        contentArea.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, requiredContentSize);
    }