LevelUp! Studio » alpha https://blog.levelup.in.th Experience the new world. Fri, 26 May 2017 10:06:07 +0000 th hourly 1 http://wordpress.org/?v=3.8.1 วิธีสร้าง Alpha Animation ใน Unity https://blog.levelup.in.th/2014/04/30/%e0%b8%a7%e0%b8%b4%e0%b8%98%e0%b8%b5%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-alpha-animation-%e0%b9%83%e0%b8%99-unity/ https://blog.levelup.in.th/2014/04/30/%e0%b8%a7%e0%b8%b4%e0%b8%98%e0%b8%b5%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-alpha-animation-%e0%b9%83%e0%b8%99-unity/#comments Wed, 30 Apr 2014 05:42:31 +0000 http://blog.levelup.in.th/?p=3339 เนื่องจาก Unity ไม่สามารถแก้ไขค่า alpha จากตัว Unity ได้โดยตรง ต้องเขียน code ใน monodev เพื่อแก้ค่า alpha เท่านั้น ผมจึงต้องหาทางแก้ไขปัญหานี้ โดยวิธีแก้มีดังนี้

สร้าง script (C#) ขึ้นมาดังนี้


using UnityEngine;
using System.Collections;
public class test11 : MonoBehaviour {
public float alpha = 0f;
void setColor(float num) {
Color color = this.renderer.material.color;
color.a = num;
this.renderer.material.color = color;
}
void update() {
setColor (alpha);
}
}

โดยหลักการก็ไม่มีอะไรมาก แค่ตัวแปร alpha ซึ่งเซ็ตเป็น public จะปรากฏใน Inspector ของ Unity ให้เซ็ตค่าได้ และสิ่งสำคัญคือต้องกำหนดเป็น float ไม่อย่างนั้นจะไม่สามารถนำตัวแปรนี้ไปใส่ทำ Animation ได้ดังภาพด้านล่าง

หลังจากใส่ลงไปแล้ว คลิก Add curve ตามรูปเพื่อเลือก Alpha โดยจะสามารถกำหนดเลข alpha ได้ตั้งแต่ 0, 0.1 ไปจนถึง 1 ซึ่ง 0 คือโปร่งใสไม่เห็นอะไรเลย และ 1 คือเห็นภาพครบถ้วนปกติ จะสามารถสร้าง animation alpha ได้โดยง่าย ซึ่งหลักการก็คือนำตัวแปร alpha ที่เราประกาศ public ไว้ไป set ค่า alpha ใน function update ตลอดเวลาเพื่อให้การแสดงผลค่าโปร่งใสเปลี่ยนทุก frame เท่านี้ก็เรียบร้อย :)

]]>
https://blog.levelup.in.th/2014/04/30/%e0%b8%a7%e0%b8%b4%e0%b8%98%e0%b8%b5%e0%b8%aa%e0%b8%a3%e0%b9%89%e0%b8%b2%e0%b8%87-alpha-animation-%e0%b9%83%e0%b8%99-unity/feed/ 0