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;

 }


沒有留言:

張貼留言