วันศุกร์ที่ 25 พฤศจิกายน พ.ศ. 2559
Go to new position with transform.localPosition แบบเป๊ะๆ
การใช้
currentMenuPanel.transform.localPosition = new Vector3 (oldPosition+3503.0f , -54.0f , 0.0f);
วันพฤหัสบดีที่ 24 พฤศจิกายน พ.ศ. 2559
Unity 3D : สร้าง class extension ให้ unity
public static class Extensions
{
public static void SetTransparency(this UnityEngine.UI.Image p_image, float p_transparency)
{
if (p_image != null)
{
UnityEngine.Color __alpha = p_image.color;
__alpha.a = p_transparency;
p_image.color = __alpha;
}
}
}
ใส่ โค้ดนี้ลงไปใน script ที่ต้องการใช้ extension
วิธีการใช้งาน
menuButtons [0].gameObject.GetComponent<Image> ().SetTransparency (0);
วันพุธที่ 23 พฤศจิกายน พ.ศ. 2559
Unity 3D : rotation same as another
rotation same as another
using UnityEngine;
using System.Collections;
public class SameRotation : MonoBehaviour {
public Camera camera;
void Start () {
}
void Update () {
transform.localRotation = camera.transform.localRotation;
}
}
Unity 3D : Go to new position with Corutine
Go to new position with Corutine
using System.Collections;
public class GoToOtherPosition : MonoBehaviour {
public Vector3 d;
int i = 0;
void Start () {
StartCoroutine ( MoveTowardsTarget() );
}
IEnumerator MoveTowardsTarget() {
for(int i = 0 ; i <= 30 ; i++)
{
//yield return new WaitForSeconds(0.005f);
yield return null;
transform.position = new Vector3 (i, 0, 0);
}
}
}
วันอาทิตย์ที่ 13 พฤศจิกายน พ.ศ. 2559
Unity 3D : Optimize on android
File Size(KB) Format Supporting Devices Alpha Channels21843 ASTC 1242 Good22759 ATC 3144 Good22860 DXT 992 Very Good23021 ETC2 10271 Average31598 Don’t Override 10271 Poor31598 ETC1 10248 Bad35933 PVRTC 2473 Bad
********** If you are using any asset store stuff I would delete "examples" comes with them & double check for "resources" folders...
วันเสาร์ที่ 12 พฤศจิกายน พ.ศ. 2559
Unity 3D : Corutines
* Credit from http://www.unitygeek.com/coroutines-in-unity3d/
Unity 3D : Corutines
yield return null – Resumes execution after All Update functions have been called, on the next frame
yield return new WaitForSeconds(t) – Resumes execution after (Approximately) t seconds
yield return new WaitForEndOfFrame() – Resumes execution after all cameras and GUI were rendered
yield return new WaitForFixedUpdate() – Resumes execution after all FixedUpdates have been called on all scripts
yield return new WWW(url) – Resumes execution after the web resource at URL was downloaded or failed.
* StopAllCorutines มันจะหยุดเฉพาะ ใน scripts เท่านั้น
using UnityEngine;
using System.Collections;
public class FirstObjectScript : MonoBehaviour {
int a,b;
bool pressed = false;
private IEnumerator coroutineBeginning;
private IEnumerator coroutineSeconding;
// Use this for initialization
void Start () {
coroutineBeginning = Beginning() ;
coroutineSeconding = Seconding() ;
StartCoroutine (coroutineBeginning);
StartCoroutine (coroutineSeconding );
}
public IEnumerator Beginning()
{
while(true)
{
Debug.Log ("In Beginning Before yield return null : " + Time.frameCount );
yield return null;
Debug.Log ("In Beginning After yield return null : " + Time.frameCount );
}
}
public IEnumerator Seconding()
{
while(true)
{
Debug.Log ("In Seconding Before yield return null : " + Time.frameCount );
yield return null;
Debug.Log ("In Seconding After yield return null : " + Time.frameCount );
}
}
// Update is called once per frame
void Update () {
if (pressed == false) {
if (Input.GetKeyDown ("space")) {
pressed = true;
StopAllCoroutines();
}
} else {
if (Input.GetKeyDown ("space")) {
pressed = false;
StartCoroutine (coroutineBeginning);
StartCoroutine (coroutineSeconding);
}
}
}
}
************************************************************
ผลที่ได้
- ก็สมเหตสมผลครับ เริ่มต้นก็ทำงานที่ frame 1 s]
- หลังจากใช้ yield return null ก็ รอจนจบ frame
- หลังจบ frame ก็ขึ้น frame 2
- ในขณะ frame 2 ก็ยังทำงาน ยุ จนกว่าจะมี คำสั่ง yield return ขึ้นมา มัน
จึงจะขึ้น frame ที่ 3 ต่อไป
![]() |
สมัครสมาชิก:
บทความ (Atom)

