Last active
August 29, 2015 14:03
-
-
Save s2kw/dfef31c162527be3a6fb to your computer and use it in GitHub Desktop.
Unityのタッチしたらエフェクト出すコンポーネント。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class TouchParticleController : MonoBehaviour { | |
public enum TouchState{ | |
Touch,Untouch | |
} | |
public Transform particleAnchor; | |
public List<ParticleSystem> particles; | |
public Camera cam; | |
public TouchState __state = TouchState.Untouch; | |
public TouchState state{ | |
set{ | |
if(this.__state == value) return; | |
this.__state = value; | |
StartCoroutine( EmissionChangeAfterOneFrame( (value == TouchState.Touch ) ); | |
} | |
get{ return this.__state;} | |
} | |
IEnumerator EmissionChangeAfterOneFrame( bool _isEmit ){ | |
yield return null; | |
particles.ForEach( c => c.enableEmission = _isEmit ); | |
} | |
// Use this for initialization | |
void Start () { | |
this.state = TouchState.Untouch; | |
if( this.cam == null ) | |
this.cam = gameObject.GetComponent<Camera>() as Camera; | |
} | |
// Update is called once per frame | |
void Update () { | |
# if UNITY_EDITOR | |
if ( Input.GetMouseButton(0) ) { | |
var pos = Input.mousePosition; | |
# else | |
if (Input.touchCount > 0 && ( Input.GetTouch(0).phase == TouchPhase.Began) || ( Input.GetTouch(0).phase == TouchPhase.Moved) ) { | |
var pos = Input.GetTouch(0).position; | |
# endif | |
Ray ray = this.cam.ScreenPointToRay(pos); | |
particleAnchor.position = new Vector3(ray.origin.x,ray.origin.y, 200f); | |
this.state = TouchState.Touch; | |
}else{ | |
this.state = TouchState.Untouch; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment