結果圖:
內容連結
2015年6月14日 星期日
2015年6月9日 星期二
Unity UI Canvas Draw
unity interface setting:
CanvasDraw(C#) :
CanvasDraw(C#) :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class CanvasDraw : MonoBehaviour {
public static Texture2D CANVAS = null;
public static RectTransform RCET_Transform; // rectTransform for unity ui
public static RawImage RAW_IMAGE; // rawImage for unity ui
// canvas size
int canvasWidth = Screen.width;
int canvasHeight = Screen.height;
void Awake(){
/***********************
* TODO Componet Setting
* Prior start Calculate
************************/
RAW_IMAGE = GetComponent<RawImage> ();
RCET_Transform = GetComponent<RectTransform> ();
RCET_Transform.anchoredPosition = new Vector2 (Screen.width / 2, Screen.height / 2);
}
// Use this for initialization
void Start () {
/*******************************
* TODO Create Canvas
* Unity UI When either Canvas
******************************/
CANVAS = new Texture2D (canvasWidth, canvasHeight);
Color[] color = new Color[CANVAS.width * CANVAS.height]; // Canvas pixel value in color array
for (int i = 0; i < color.Length; i++) // read canvas value
color [i] = Color.white; // read canvas pixel color white
CANVAS.SetPixels (color); // write canvas setPixels
CANVAS.Apply (); // canvas Apply
RAW_IMAGE.texture = CANVAS; // canvas for rawimage
// Canvas SetSize
RCET_Transform.sizeDelta = new Vector2 (CANVAS.width, CANVAS.height);
RCET_Transform.pivot = new Vector2 (0.5f, 0.5f);
}
}
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class CanvasDraw : MonoBehaviour {
public static Texture2D CANVAS = null;
public static RectTransform RCET_Transform; // rectTransform for unity ui
public static RawImage RAW_IMAGE; // rawImage for unity ui
// canvas size
int canvasWidth = Screen.width;
int canvasHeight = Screen.height;
void Awake(){
/***********************
* TODO Componet Setting
* Prior start Calculate
************************/
RAW_IMAGE = GetComponent<RawImage> ();
RCET_Transform = GetComponent<RectTransform> ();
RCET_Transform.anchoredPosition = new Vector2 (Screen.width / 2, Screen.height / 2);
}
// Use this for initialization
void Start () {
/*******************************
* TODO Create Canvas
* Unity UI When either Canvas
******************************/
CANVAS = new Texture2D (canvasWidth, canvasHeight);
Color[] color = new Color[CANVAS.width * CANVAS.height]; // Canvas pixel value in color array
for (int i = 0; i < color.Length; i++) // read canvas value
color [i] = Color.white; // read canvas pixel color white
CANVAS.SetPixels (color); // write canvas setPixels
CANVAS.Apply (); // canvas Apply
RAW_IMAGE.texture = CANVAS; // canvas for rawimage
// Canvas SetSize
RCET_Transform.sizeDelta = new Vector2 (CANVAS.width, CANVAS.height);
RCET_Transform.pivot = new Vector2 (0.5f, 0.5f);
}
}
----------------------------------
TestPaint(C#) :
using UnityEngine;
using System.Collections;
public class TestPaint : MonoBehaviour {
public Color paintColor = Color.red; // paint color
Vector2 oldSetPixelVector2; // after write pixel
public float zoom = 1, rotation = 0;
// Update is called once per frame
void Update () {
/**********************************
* TODO Paint
* Mouset when either Paint
*********************************/
if(Input.GetMouseButton(0)){
if(CanvasDraw.CANVAS != null){
float offsetX = (CanvasDraw.RCET_Transform.anchoredPosition.x - (CanvasDraw.CANVAS.width * zoom)/2);
float OffsetY = (CanvasDraw.RCET_Transform.anchoredPosition.y - (CanvasDraw.CANVAS.height * zoom)/2);
float x = (Input.mousePosition.x - offsetX)/zoom;
float y = (Input.mousePosition.y - OffsetY)/zoom;
Vector2 v = RotateSquare(x, y, CanvasDraw.CANVAS.width, CanvasDraw.CANVAS.height, rotation/(180/Mathf.PI));
float dis = Vector2.Distance(oldSetPixelVector2, v);
float split = 1/ dis;
if(!Input.GetMouseButtonDown(0)){
for(float i = 0; i<1; i+=split){
Vector2 paintSetPixel = Vector2.Lerp(oldSetPixelVector2, v, i); // Paint point x,y lerp
CanvasDraw.CANVAS.SetPixel((int)paintSetPixel.x, (int)paintSetPixel.y, paintColor); // Paint SetCanvas Pixel
}
}
CanvasDraw.CANVAS.Apply(); // canvas write pixel
oldSetPixelVector2 = v; // paint vector2D posistion
}
}
}
Vector2 RotateSquare(float X, float Y, int W, int H, float phi){
float sin = Mathf.Sin(phi), cos = Mathf.Cos(phi);
int xc = W/2, yc = H/2;
int x = (int)(cos*(X-xc)+sin*(Y-yc)+xc);
int y = (int)(-sin*(X-xc)+cos*(Y-yc)+yc);
return new Vector2(x, y);
}
}
----------------------------------
結果圖:
訂閱:
文章 (Atom)