วันศุกร์ที่ 2 ธันวาคม พ.ศ. 2559

Shooting โค้ดยิงกระสุนนน



using UnityEngine;
using System.Collections;

public class Shootiing : MonoBehaviour {

public GameObject bulletPrefab;
public float bulletImpulse = 100.0f;

public GameObject CameraObject;
// Use this for initialization
void Start () {

}

public void FireOut()
{
GameObject theBullet = (GameObject)Instantiate (bulletPrefab , CameraObject.transform.position , CameraObject.transform.rotation);
theBullet.GetComponent<Rigidbody> ().AddForce ( CameraObject.transform.forward * bulletImpulse , ForceMode.Impulse);
}

public void NotFireOut()
{

}

// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Fire1"))
{
FireOut ();
}
}
}

EnemyScript VR มองแล้วยยิงศัตรูอัตโนมัติ



using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using VRStandardAssets.Utils;


namespace VRStandardAssets.Menu
{

public class EnamyScript : MonoBehaviour {

public event Action<EnamyScript> OnEnamySelected;                   // This event is triggered when the selection of the button has finished.

[SerializeField] private VRCameraFade m_CameraFade;                 // This fades the scene out when a new scene is about to be loaded.

[SerializeField] private SelectionRadial m_SelectionRadial;         // This controls when the selection is complete.

[SerializeField] private VRInteractiveItem m_InteractiveItem;       // The interactive item for where the user should click to load the level.




// ************************** SUA modified add **********************************
public delegate void ActivateEnemyDelegate(int paramIndex);

public ActivateEnemyDelegate activateEnamyDelegate;

[SerializeField] private int paramIndex = -1;

public Shootiing shooting;



private bool m_GazeOver;                                            // Whether the user is looking at the VRInteractiveItem currently.


private void OnEnable ()
{
m_InteractiveItem.OnOver += HandleOver;
m_InteractiveItem.OnOut += HandleOut;
m_SelectionRadial.OnSelectionComplete += HandleSelectionComplete;

// Additional Modify
m_InteractiveItem.OnOver += m_SelectionRadial.HandleDown;
m_InteractiveItem.OnOut += m_SelectionRadial.HandleUp;
}


private void OnDisable ()
{
m_InteractiveItem.OnOver -= HandleOver;
m_InteractiveItem.OnOut -= HandleOut;
m_SelectionRadial.OnSelectionComplete -= HandleSelectionComplete;

// Additional Modify
m_InteractiveItem.OnOver += m_SelectionRadial.HandleDown;
m_InteractiveItem.OnOut += m_SelectionRadial.HandleUp;
}


private void HandleOver()
{
shooting.FireOut ();

m_GazeOver = true;
}


private void HandleOut()
{

shooting.NotFireOut();

m_GazeOver = false;
}


private void HandleSelectionComplete()
{
// If the user is looking at the rendering of the scene when the radial's selection finishes, activate the button.
if(m_GazeOver)
StartCoroutine (ActivateWithEnamy());
}


private IEnumerator ActivateWithEnamy()
{
// If the camera is already fading, ignore.
if (m_CameraFade.IsFading)
yield break;

// If anything is subscribed to the OnButtonSelected event, call it.
if (OnEnamySelected != null)
OnEnamySelected(this);

Debug.Log ("Param Index : " + paramIndex);


}
}

}

การทำกล้องหมุน 360 องศา โดยใช้เมาส์

using UnityEngine;
using System.Collections;

public class FirstPersonController : MonoBehaviour {

public float mouseSensitivity = 2.0f;

public float upDownRenge = 60.0f;

private float verticalRotation = 0.0f;

public GameObject leftCamera;
public GameObject rightCamera;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

float rotationLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity ;
transform.Rotate (0,rotationLeftRight ,0);

verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity ;
verticalRotation = Mathf.Clamp (verticalRotation , -upDownRenge , upDownRenge);
leftCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);
rightCamera.transform.localRotation = Quaternion.Euler (verticalRotation , 0 , 0);


}
}

วันศุกร์ที่ 25 พฤศจิกายน พ.ศ. 2559

วันพฤหัสบดีที่ 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 UnityEngine;
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



  1. File Size(KB) Format Supporting Devices Alpha Channels
  2. 21843 ASTC 1242 Good
  3. 22759 ATC 3144 Good
  4. 22860 DXT 992 Very Good 23021 ETC2 10271 Average
  5. 31598 Don’t Override 10271 Poor
  6. 31598 ETC1 10248 Bad
  7. 35933 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 ต่อไป