2015년 4월 15일 수요일

unity GUI를 이용한 file explorer

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System;
using System.Collections.Generic;


public class FinderWindow : EditorWindow
{
    [MenuItem("Game/Finder Window")]
    public static void Init()
    {
        var window = EditorWindow.GetWindow<FinderWindow>(false, "Finder");
        window.Show();
    }

    string[] m_Directories;
    string[] m_Files;
    string m_SelectionPath;
    string m_PathButtonName;
    Rect m_RectTextField;
    Rect m_RectPathButton;
    Rect m_RectBeginArea;
    Rect m_RectSelectButton;
    Rect m_RectCancelButton;
    List<String> m_Items;
    List<String> m_Path;
    Vector2 m_ScrollVector;
    GUIStyle[] m_LabelStyle;
    bool m_StyleCheck;
    int m_SelectNum;


    void OnEnable()
    {
        m_Path = new List<string>();
        m_Items = new List<string>();
        m_Directories = new String[m_Items.Count];
        m_Files = new String[m_Items.Count];
        m_SelectionPath = "";
        m_PathButtonName = "...";
        m_RectTextField = new Rect(10, 10, 400, 20);
        m_RectPathButton = new Rect(420, 10, 30, 20);
        m_RectBeginArea = new Rect(10, 40, 440, 300);
        m_RectSelectButton = new Rect(390, 350, 60, 20);
        m_RectCancelButton = new Rect(325, 350, 60, 20);
        m_StyleCheck = false;
        m_SelectNum = -1;

    }

    void OnDestroy()
    {

    }

    void Update()
    {

    }

    void OnGUI()
    {

        GUI.TextField(m_RectTextField, m_SelectionPath);

        if (GUI.Button(m_RectPathButton, m_PathButtonName))
        {
            string folder_path = EditorUtility.OpenFolderPanel("Select Floder", "", "");

            m_SelectionPath = folder_path;

            if (m_SelectionPath != "")
            {
                FileList(m_SelectionPath);
            }

        }

        GUILayout.BeginArea(m_RectBeginArea, GUI.skin.box);
        m_ScrollVector = GUILayout.BeginScrollView(m_ScrollVector);

        if (m_Items.Count != 0)
        {
            if (GUILayout.Button("...", GUI.skin.label))
            {
                m_SelectionPath = RemoveLastWord(m_SelectionPath);

                if (m_SelectionPath != "")
                {

                    FileList(m_SelectionPath);
                }
            }
        }

        if (m_Items.Count != 0 && !m_StyleCheck)
        {
            m_LabelStyle = new GUIStyle[m_Items.Count];

            m_StyleCheck = true;
        }

        for (int i = 0; i < m_Items.Count; i++)
        {
            if (m_LabelStyle[i] == null)
            {
                m_LabelStyle[i] = new GUIStyle();
            }

            if (GUILayout.Button(m_Items[i], m_LabelStyle[i]))
            {
                if (m_LabelStyle[i].normal.textColor == Color.magenta)
                {
                    m_LabelStyle[i].normal.textColor = Color.black;

                    m_SelectionPath = m_SelectionPath + "/" + m_Items[i];

                    m_StyleCheck = false;
                    if (Directory.Exists(m_SelectionPath))
                    {
                        FileList(m_SelectionPath);
                    }
                    else
                    {
                        m_SelectionPath = RemoveLastWord(m_SelectionPath);
                    }
                    m_SelectNum = -1;

                    break;
                }
                else
                {
                    for (int j = 0; j < m_Items.Count; j++)
                    {
                        m_LabelStyle[j].normal.textColor = Color.black;
                    }
                    m_LabelStyle[i].normal.textColor = Color.magenta;
                    m_SelectNum = i;
                }
            }

        }
        GUILayout.EndScrollView();

        GUILayout.EndArea();

        if (GUI.Button(m_RectSelectButton, "Select"))
        {
            if (m_SelectNum != -1)
            {
                Debug.Log(m_Path[m_SelectNum]);
            }
        }

        if (GUI.Button(m_RectCancelButton, "Cancel"))
        {

        }
    }

    void FileList(string path)
    {
        m_Items.Clear();
        m_Path.Clear();
        m_StyleCheck = false;
       
         m_Directories = Directory.GetDirectories(path);
            for (int i = 0; i < m_Directories.Length; i++)
            {
                // Debug.Log(m_Directories[i]);
                int subNum = m_Directories[i].IndexOf('\\');
                m_Directories[i] = m_Directories[i].Replace('\\', '/');
                m_Path.Add(m_Directories[i]);
                if (subNum == -1)
                {
                    m_Items.Add(GetLastWord(m_Directories[i]));
                }
                else
                {
                    string dirName = m_Directories[i].Substring(subNum + 1);

                    m_Items.Add(dirName);
                }
            }
            m_Files = Directory.GetFiles(path);
            for (int i = 0; i < m_Files.Length; i++)
            {
                int subNum = m_Files[i].IndexOf('\\');
                m_Files[i] = m_Files[i].Replace('\\', '/');
                m_Path.Add(m_Files[i]);
                if (subNum == -1)
                {
                    m_Items.Add(GetLastWord(m_Files[i]));
                }
                else
                {
                    string fileName = m_Files[i].Substring(subNum + 1);
                    m_Items.Add(fileName);
                }
                //Debug.Log(m_Files[i]);
            }
       
    }

    public static string GetLastWord(string text)
    {
        string[] lastWord = text.Split('/');


        return lastWord[lastWord.Length - 1];
    }

    public static string RemoveLastWord(string text)
    {
        string[] lastWord = text.Split('/');
        string word = GetLastWord(text);

        if (word == text)
        {
            return text;
        }

        text = null;

        for (int i = 0; i < lastWord.Length; i++)
        {
            if (i == lastWord.Length - 1)
            {
                break;
            }
            if (i == lastWord.Length - 2)
            {
                text += lastWord[i];
            }
            else
            {
                text += lastWord[i] + "/";
            }

        }

       // Debug.Log(text);

        return text;
    }
}



2015년 4월 14일 화요일

top down RPG 예제

2.5D 형식
http://singletechgames.com/2014/08/19/tutorial-de-unity-2-5d-como-crear-un-juego-isometrico-estilo-diablo-vista-top-down/



비행기
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/top-down-2d