2014年12月22日 星期一

Unity canvas 畫線

結果圖:

Unity Web Player | New Unity Project
----------------------------------------------------------------------------------
這裡我先用javaScript 呈現,C# 出了一些很嚴重的CanVas的問題。


OpenString 腳本(按鈕控制) :

#pragma strict


var locked : boolean = true;

var colorNumberSw : int = 0;


function OnGUI(){

if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height, 100, 50),"Open canvas")){
locked = !locked;
}
if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height + 60, 100, 50),"Red")){
colorNumberSw = 1;
}
if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height + 120, 100, 50),"Green")){
colorNumberSw = 2;
}
if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height + 180, 100, 50),"Yellow")){
colorNumberSw = 3;
}
if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height + 240, 100, 50), "Return Game")){
Application.LoadLevel("CanVasTest");
}
if(GUI.Button(Rect(Screen.width/Screen.width, Screen.height/Screen.height + 300, 100, 50), "Exit Game")){
Application.Quit();
}
}


--------------------------------------------------------------------

Driver (畫線啟動腳本) :

#pragma strict


 private var cc : canvas;


// private var locked : boolean = true;
 var testopenString : OpenString;


 // get the size of the screen

 private var pixels = Screen.width * Screen.height;

 // create a color array
 private var colors = new Color[pixels];

 private var pencolor = Color.black;

 // counter is used to avoid setting pixels on each onGUI call
 private var counter = 101;

 // last detected mouse position
 private var lastpos : Vector2;

 private var reset : boolean = true;

 function Start() {

     cc = GetComponent(canvas);

     // fill color array with a BG color
     for (var i = 0; i < pixels; i++) {
         colors[i] = Color(1.0, 1.0, 1.0, 0.45); // Color.white; // canvasBackground.GetPixel(0, 0);
     }

 }

 function Update() {

  if(testopenString.colorNumberSw == 1)pencolor = Color.red;
  if(testopenString.colorNumberSw == 2)pencolor = Color.green;
  if(testopenString.colorNumberSw == 3)pencolor = Color.yellow;

     if (testopenString.locked) {
         cc.draw = false;
         return;
     }
     
     cc.draw = true;

     // mouse 0 'paints'
     if (Input.GetMouseButton(0)) {

         var currpos : Vector3 = Input.mousePosition;

         if (reset) {
             reset = false;
             lastpos = currpos;
         }

         line(lastpos.x, lastpos.y, currpos.x, currpos.y);
         lastpos = currpos;

         counter++;

         if (counter > 3) {

             counter = 0;

             cc.setColors(colors);
         }

     } else {
         reset = true;
     }
 }

 // Bresenham's line algorithm
 function line(x0 : int, y0 : int, x1 : int, y1 : int) {
     var dx = Mathf.Abs(x1 - x0);
     var dy = Mathf.Abs(y1 - y0);
     var sx = (x0 < x1) ? 1 : -1;
     var sy = (y0 < y1) ? 1 : -1;
     var err = dx - dy;
     while (true) {

         // Do what you need to for this
         // setPixel(x0,y0);
         colors[x0 + Screen.width * y0] = pencolor;

         if ((x0 == x1) && (y0 == y1))
             break;
         var e2 = 2 * err;
         if (e2 > -dy) {
             err -= dy;
             x0 += sx;
         }
         if (e2 < dx) {
             err += dx;
             y0 += sy;
         }
   
     }
 }

------------------------------------------------

Canvas (畫佈 設定 腳本) :

#pragma strict


 public var draw : boolean = false;

 private var canvasBackground : Texture2D;
 canvasBackground = new Texture2D(Screen.width, Screen.height);

 var width : int;
 var height : int;

 // initialize the canvas


 function setColors(newColors:Color[]) {
     canvasBackground.SetPixels(newColors, 0); 
     canvasBackground.Apply(false);
 }

 function OnGUI() {

     if (!draw)
         return;
         
     GUI.Box(Rect(0, 0, canvasBackground.width-width, canvasBackground.height-height), "");
     GUI.skin.box.normal.background = canvasBackground;

 }


2014年12月9日 星期二

Unity 計時器 Damo

結果呈現:





--------------------------------------------------------------------------------------------
發射物件:

using UnityEngine;
using System.Collections;

public class Fire : MonoBehaviour
{
int timer_i = 0;
public GameObject fireGameonbjcet;
public int fireSw;
// Update is called once per frame
void time4 ()
{
if (fireSw == 1) {
Debug.Log ("1");
timer_i += 1;
Instantiate (fireGameonbjcet, transform.position, transform.rotation);
}
}
void Start ()
{
InvokeRepeating ("time4", 1f, 2f);
}

void OnGUI ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) {
fireSw = 1;
} else if (Input.GetKeyDown (KeyCode.Alpha2)) {
fireSw = 2;
}
if (fireSw == 2) {
GUI.Label(new Rect(Screen.width/Screen.width, Screen.height/Screen.height, 400 ,50),"請案下數字鍵 1: 自動生產物件");
if (Input.GetMouseButtonDown(0)) {
Instantiate (fireGameonbjcet, transform.position, transform.rotation);
}
}else if(fireSw == 0){
GUI.Label(new Rect(Screen.width/Screen.width, Screen.height/Screen.height, 400 ,50),"請案下數字鍵 1: 自動生產物件,2: 滑鼠左鍵生產物件");
}else if(fireSw == 1){
GUI.Label(new Rect(Screen.width/Screen.width, Screen.height/Screen.height, 400 ,50),"請案下數字鍵 2: 滑鼠左鍵生產物件");
}
}
}

--------------------------------------------------------------------------------------------
物件行為:

using UnityEngine;
using System.Collections;

public class GameobjectTest : MonoBehaviour {
public float speed;
// Update is called once per frame
void Update () {
transform.Translate(-Vector3.forward * Time.deltaTime * speed);
Destroy(gameObject, 2f);
}
}