2016年8月30日 星期二

Unity Editor視窗建立遊戲物件

Script(C#):

using UnityEngine;
using System.Collections;
using UnityEditor;

public class Windows : EditorWindow {

    private GUIStyle stlye = new GUIStyle();

    [MenuItem("遊戲控制器視窗/2D/RPG MAKER")]
    static void Init() {
        Windows window = (Windows)EditorWindow.GetWindow(typeof(Windows));
        window.Show();
    }

    void OnGUI() {

        // GUI 風格
        stlye.fontSize = 18;
        stlye.normal.textColor = Color.white;

        // 背景圖片
        EditorGUILayout.BeginHorizontal();
        if (GUI.Button(new Rect(5, 30, 100, 30), GUIContent.none))
            backGroundFunction();

        GUILayout.BeginArea(new Rect(5, 30, 100, 30));
        GUILayout.Label("背景圖片", stlye);
        GUILayout.EndArea();
        EditorGUILayout.EndHorizontal();
    }

    void backGroundFunction() {
        GameObject backGround = new GameObject("背景圖片");
    }
}

----------------------------
結果圖:



2016年8月18日 星期四

Unity C# 以字串分割找到自己的想要的關鍵字

Script (C#):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Vocabulary : MonoBehaviour {

    private int mood;        // 心情變數
    public int moodType {
        get { return mood; }
    }

    Regex biologicalHerbivorousReget, biologicalCarnivorousReget, biologicalOmnivoreReget;  // 生物字串分割
    Regex personalRegex; // 人稱字串分割
    Regex familyTitleRegex; // 家族職稱字串分割

    /// <summary>
    /// 家族職稱類方法
    /// </summary>
    /// <param 句子="家族職稱關鍵字"></param>
    public void familyTitleFunction(string familyTileName) {

        string[] familyTitleType = new string[] { "哥", "弟", "姊", "妹", "爸", "媽", "奶", "公", "嬤", "奶奶", "爺", "叔", "叔叔", "舅" };

        // list 加入家族職稱陣列裡面數值
        List<string> familTiles = new List<string>();
        for (int x = 0; x < familyTitleType.Length; x++)
            familTiles.Add(familyTitleType[x]);

        // 字串分割之後判斷是否家族職稱
        foreach (string result in familTiles){
            familyTitleRegex = new Regex(result);
            string[] splitResult = familyTitleRegex.Split(familyTileName);

            foreach (string splitResultType in splitResult)
                if (familyTileName != splitResultType)
                    Debug.Log(familyTitleRegex.ToString());
        }
    }

    /// <summary>
    /// 人稱類方法
    /// </summary>
    /// <param 句子="人稱關鍵字"></param>
    public void personalNounFunction(string personalName) {

        string[] personalType = new string[] { "你", "我", "他", "您", "咱", "妳", "您", "她", "它" };

        // list 加入人稱陣列裡面數值
        List<string> personalNouns = new List<string>();
        for (int x = 0; x < personalType.Length; x++)
            personalNouns.Add(personalType[x]);

        // 字串分割之後判斷是否人稱
        foreach (string result in personalNouns) {
            personalRegex = new Regex(result);
            string[] splitResult = personalRegex.Split(personalName);

            foreach (string splitResultType in splitResult)
                if (personalName != splitResultType)
                    Debug.Log("人稱");
        }
    }

    /// <summary>
    /// 生物類方法(雜食、草食、肉食)
    /// </summary>
    /// <param 句子="動物關鍵字"></param>
    public void biologicalFunction(string animalName) {
    
        string[] biologicalHerbivorousType = new string[] { "羊", "牛", "馬", "鹿", "象", "兔", "駝"};
        string[] biologicalCarnivorousType = new string[] { "獅", "虎", "豹", "貓", "狼", "鱷", "鷹", "蛇", "鯊", "熊", "鯨", "食人" };
        string[] biologicalOmnivoreType = new string[] { "人", "猴", "猩", "雞", "鳥", "豬", "鼠", "蜥", "鴨", "雀", "蛙", "犬", "狗" };

        // list 加入草食性生物陣列裡面數值
        List<string> animalHerbivorous = new List<string>();
        for (int x = 0; x < biologicalHerbivorousType.Length; x++)
            animalHerbivorous.Add(biologicalHerbivorousType[x]);

        // 字串分割之後判斷是否草食性生物
        foreach (string result in animalHerbivorous){
            biologicalHerbivorousReget = new Regex(result);
            string[] splitResult = biologicalHerbivorousReget.Split(animalName);

            foreach (string splitResultType in splitResult)
                if (animalName != splitResultType)
                    Debug.Log("草食性生物");
                    //mood = 1;
        }

        // list 加入肉食性生物陣列裡面數值
        List<string> animalCarnivorous = new List<string>();
        for (int x = 0; x < biologicalCarnivorousType.Length; x++)
            animalCarnivorous.Add(biologicalCarnivorousType[x]);

        // 字串分割之後判斷是否肉食性生物
        foreach (string result in animalCarnivorous){
            biologicalCarnivorousReget = new Regex(result);
            string[] splitResult = biologicalCarnivorousReget.Split(animalName);

            foreach (string splitResultType in splitResult)
                if (animalName != splitResultType)
                    Debug.Log("肉食性生物");
        }

        // List 加速雜食性生物陣列裡面數值
        List<string> biologicalOmnivore = new List<string>();
        for (int x = 0; x < biologicalOmnivoreType.Length; x++)
            biologicalOmnivore.Add(biologicalOmnivoreType[x]);

        foreach (string result in biologicalOmnivore) {
            biologicalOmnivoreReget = new Regex(result);
            string[] splitResult = biologicalOmnivoreReget.Split(animalName);

            foreach (string splitResultType in splitResult)
                if (animalName != splitResultType)
                    Debug.Log("雜食性生物");
        }
    }
}
------------------------------

輸入 他是我爸


找出: 爸 關鍵字



2016年6月15日 星期三

基數排序

基數排序

       基數排序(Radix sort)原理是將整數按位數切割成不同的數字,然後按每個位數分別比較。屬於一種分配模式排序方式. 基數排序法依比較的方向可分為最有效鍵優先(Most Significant Digit First, MSD)和最無效鍵優先 (Least Significant Digit First, LSD)兩種,MSD法是從最左邊的位數開始比較,而LSD則是從最右邊的位數開始比較。

C# 語法



合併排序

合併排序

      合併排序(Merge sort) 使用分治法(Divide and Conquer)的演算法來實作。排序時需要額外的空間來處理。

      分治法: 建基於多項分支遞歸的一種很重要的算法範式。

      分治法之優點:

  1.       將困難的問題簡化為容易實作的方式。
  2.       提升程式效率。
  3.       能夠平行處理。


C# 語法 

快速排序

快速排序

       快速排序(Quicksort)原理從數列中挑選一個基準點,大於基準的放一邊,小於的放一邊,如此循環最後可完成排序。

快速排序演算法的運作:
  1. 數列中選擇一元素作為基準點。
  2. 小於元素的移至基準的左邊,大於元素的移至右邊,相等的任意放。
  3. 基準點左邊和右邊視為兩個數列,並重複過程直到剩下一個或零。


實作範例C#  語法












2016年6月14日 星期二

氣泡排序

氣泡排序

       氣泡排序(Bubble Sort)原理重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。

氣泡排序演算法的運作:
  1. 比較相鄰的兩個元素,若前面的元素較大就進行交換。
  2. 重複進行1的動作直到最後面,最後一個元素將會是最大值。
  3. 重複進行1,2的動作,每次比較到上一輪的最後一個元素。
  4. 重複進行以上動作直到沒有元素需要比較。

實作範例:

C# 語法














JAVA 語法


插入排序

插入排序

       插入排序(Insertion Sort)原理是通過構建有序序列,對於未排序數據,在已排序序列中從後 向前掃描,找到相應位置並插入,插入排序在實現上,通常採用in-place排序,因而在從後向前掃描過程中,需要反覆把已排序元素逐步向後挪位,為最新元素提供插入空間,在與已排序的部分比較時,利用指派的方式將元素往右位移,來達到插入的效果,因為位移的關係需要有另一個變數來暫存最右邊的數值。 

       排序的部分比較時,利用指派的方式將元素往右位移,來達到插入的效果,因為位移的關係需要有另一個變數來暫存最右邊的數值。
運算流程:   
  1. 將未排序的部分最左邊元素儲存到暫存變數。 
  2. 由後往前和已排序部分元素比較,若大則將該元素往右邊位移。 
  3. 重複2的動作直到遇到不大於自己的元素,將暫存變數寫入在該元素之後;若都沒有則寫入在最前面。

實作範例

C# 語法


JAVA語法