2016年4月8日 星期五

按下滑鼠左鍵,動態生成物件

1.首先必須先建立一個 空物件(Create Empty)

2.再建立一個想自動重複生成的物件(例如:Cube...)

3.撰寫程式碼
程式碼如下:

using UnityEngine;
using System.Collections;

public class control MonoBehaviour
{  public GameObject box;
    void Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        {//如果按下滑鼠左鍵
            Instantiate(box,transform.position,transform.rotation);
            //動態生成(複製來源,出現位置(座標),出現方向(角度))
        }

     }
}

4.將該script檔加給 空物件

5.修改空物件此script的box屬性值:將Cube拖曳至box屬性值欄位

滑鼠點擊/滑進/滑出物件

程式碼如下:

using UnityEngine;
using System.Collections;

public class objClick MonoBehaviour
{
    void OnMouseEnter()
    {//滑鼠滑進後,物件顏色改變為紅色
        GetComponent<Renderer>().material.color = Color.red;
    }
    void OnMouseDown()
    {//滑鼠點擊後,出現物件名稱+被點擊
        Debug.Log(name.ToString() + "被點擊");
    }

    void OnMouseExit()
    {//滑鼠滑出後,物件顏色改變為白色
        GetComponent<Renderer>().material.color = Color.white;
    }
}

背景移動

程式碼如下:

using UnityEngine;
using System.Collections;

public class starFieldScroll : MonoBehaviour
{
    public float scrollSpeed;//背景移動速度
    void Update () {
        GetComponent<Renderer>().material.mainTextureOffset = new  Vector2(0,Time.time * scrollSpeed);//背景移動;主纹理偏移(x軸不動,y軸移動(以秒為單位))
    }
}


.htaccess應用

去除WWW RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com $ [ NC ] RewriteRule ^(.*) $ https: / /example.com/ $1 [ R = 301...