Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class DeviceSensors : MonoBehaviour {
- public Vector3 acceleration; //Input.acceleration
- public Vector3 gyroUnbiased;
- public Quaternion gyroAttitude;
- public Dictionary<int,string> touchRepeatDic;
- private IEnumerator coroutine;
- private Color red = Color.red;
- private Color originColor;
- public xInputType sensorType = xInputType.TOUCHPAD;
- public GameObject touchArea;
- public Vector2 touchPos= Vector2.zero;
- private GameObject gamepadObj;
- private GameObject touchpadObj;
- public LineRenderer _renderer;// = gameObject.GetComponent<LineRenderer>();
- //low pass fliler
- static float AccelerometerUpdateInterval = 1.0f / 60.0f;
- static float LowPassKernelWidthInSeconds= 1.0f;
- private float LowPassFilterFactor =AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
- //private Vector3 lowPassValue = Vector3.zero;
- // Use this for initialization
- void Start () {
- #if UNITY_EDITOR
- Resolution.Instance.SetScreenWidthAndHeightFromEditorGameViewViaReflection ();
- float w = (float)Resolution.Instance.ScreenWidth;
- float h = (float)Resolution.Instance.ScreenHeight;
- #else
- float w = Screen.width;
- float h = Screen.height;
- #endif
- float s = (w > h ? h:w )/ 5.625f;//192.0f;
- float size = w / ((( w / h ) * 2 ) * s );//5.625f;//
- //Debug.Log ("調整" + (float)Resolution.Instance.ScreenWidth / (float)Resolution.Instance.ScreenHeight);
- if (w/ h > 0.60f) {
- size *= 1.2f;
- }
- Debug.Log ("Start DeviceSensors w:" +w + "/h:" + h + "/s:" + s + "/size:" + size) ;
- //タブレット端末など
- //float s = Screen.height / 5.625f;//192.0f;
- //float size = h / ((( h / w ) * 2 ) * s );
- Camera.main.orthographicSize = size;
- red = ColorUtils.ToRGB (0xC83FB3);
- originColor = GameObject.Find ("ButtonA").transform.GetComponents<Renderer> () [0].materials [0].color;
- touchRepeatDic = new Dictionary<int,string>();
- acceleration = Input.acceleration;
- gyroUnbiased = Input.gyro.rotationRateUnbiased;
- if (SystemInfo.supportsGyroscope) {
- Input.gyro.enabled = true;
- } else {
- Debug.Log ("no support gyro.");
- }
- //Compass
- Input.compass.enabled = true;
- gamepadObj = GameObject.Find ("GamePad");
- touchpadObj= GameObject.Find ("TouchPad");
- changeSensorType (sensorType);
- _renderer= gameObject.GetComponent<LineRenderer>();
- // 線の幅
- _renderer.SetWidth(0.1f, 0.1f);
- // 頂点の数
- }
- // Update is called once per frame
- void Update () {
- if(sensorType == xInputType.OFF)
- return;
- // get acceleration
- //acceleration = Input.acceleration;
- acceleration = LowPassFilterAccelerometer ();
- gyroUnbiased = Input.gyro.rotationRateUnbiased;
- // Debug.Log ("acceleration" + dir);
- // get gyro attitude
- if (Input.gyro.enabled) {
- gyroAttitude = Input.gyro.attitude;
- /*
- gyroAttitude.x *= -1;
- gyroAttitude.y *= -1;
- gyroAttitude.z *= 1;
- */
- }
- // get Conpass
- transform.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);
- #if ( UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8 ) && UNITY_EDITOR
- /* Touch Controlls*/
- Touch[] _touch = Input.touches;
- if(_touch.Length == 0 ){
- touchPos = Vector2.zero;
- _renderer.enabled = false;
- if( touchRepeatDic.Count > 0){
- Debug.Log("FORCE RESET ");
- foreach(var d in touchRepeatDic){
- coroutine = ChangeColorOfGameObject (GameObject.Find(d.Value).gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- touchRepeatDic.Remove(d.Key);
- }
- }
- }
- else{
- foreach(var touch in _touch){
- //changed
- if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Began){
- Ray ray = Camera.main.ScreenPointToRay (touch.position);
- RaycastHit hit = new RaycastHit ();
- if (Physics.Raycast (ray, out hit)) {
- GameObject obj = hit.collider.gameObject;
- if(obj == touchArea ){
- drawTouchedLine(Camera.main.ScreenToWorldPoint(touch.position) );
- }
- SendMessage("OscSendTouchBegin", obj.name);
- touchRepeatDic[touch.fingerId] = obj.name;
- coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
- StartCoroutine ( coroutine);
- }
- }
- if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Moved){
- Ray ray = Camera.main.ScreenPointToRay (touch.position);
- RaycastHit hit = new RaycastHit ();
- if (Physics.Raycast (ray, out hit)) {
- GameObject obj = hit.collider.gameObject;
- if(obj == touchArea ){
- drawTouchedLine(Camera.main.ScreenToWorldPoint(touch.position) );
- }
- if(touchRepeatDic.ContainsKey(touch.fingerId) == false){
- // Debug.Log ("KEYSTART"+ obj.name);
- SendMessage("OscSendTouchBegin", obj.name);
- touchRepeatDic[touch.fingerId] = obj.name;
- }
- //slide another button
- else if(touchRepeatDic[touch.fingerId] != obj.name ){
- coroutine = ChangeColorOfGameObject (GameObject.Find(touchRepeatDic[touch.fingerId]).gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- SendMessage("OscSendTouchEnd", touchRepeatDic[touch.fingerId]);
- //touchState[Input.GetTouch(i).fingerId] = "";
- touchRepeatDic.Remove(touch.fingerId);
- }
- touchRepeatDic[touch.fingerId] = obj.name;
- coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
- StartCoroutine ( coroutine);
- }
- }
- if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Ended || Input.GetTouch(touch.fingerId).phase == TouchPhase.Canceled ){
- Debug.Log("END1:" +touch.fingerId );
- if(touchRepeatDic[touch.fingerId] != null ){
- Debug.Log("END2:" +touch.fingerId + ":" + touchRepeatDic[touch.fingerId] );
- if(touchRepeatDic[touch.fingerId] == touchArea.name ){
- touchPos = Vector2.zero;
- _renderer.enabled = false;
- }
- SendMessage("OscSendTouchEnd", touchRepeatDic[touch.fingerId]);
- coroutine = ChangeColorOfGameObject (GameObject.Find(touchRepeatDic[touch.fingerId]).gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- touchRepeatDic.Remove(touch.fingerId);
- }
- }
- }
- }
- #else
- /* Keyboard Controlls*/
- int c =0;
- foreach (var k in xMultiInputs.xKeymap.Keys){
- if(Input.GetKeyDown(k)){ //keydown
- try{
- GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
- SendMessage("OscSendTouchBegin", obj.name);
- coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
- StartCoroutine ( coroutine);
- }catch(UnityException e){}
- }
- else if(Input.GetKeyUp(k)){ //keyup
- try{
- GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
- coroutine = ChangeColorOfGameObject (obj.gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- SendMessage("OscSendTouchEnd", obj.name);
- }catch(UnityException e){}
- }
- else if(Input.GetKey (k)){ //repeat
- //Debug.Log (xMultiInputs.xKeymap[k]);
- try{
- GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
- touchRepeatDic[c] = obj.name;
- coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
- StartCoroutine ( coroutine);
- c++;
- }catch(UnityException e){
- }
- }
- }
- //delete previous repeat dict.
- for (int s = touchRepeatDic.Count - 1; s >= c; s--) {
- touchRepeatDic.Remove(s);
- }
- if(sensorType == xInputType.TOUCHPAD){
- if(Input.GetMouseButtonUp(0)){
- touchPos = Vector2.zero;
- _renderer.enabled = false;
- SendMessage("OscSendTouchEnd", touchArea.name);
- }
- else if(Input.GetMouseButtonDown(0)){
- SendMessage("OscSendTouchBegin", touchArea.name);
- }
- else if(Input.GetMouseButton(0)){
- Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
- RaycastHit hit = new RaycastHit ();
- if (Physics.Raycast (ray, out hit)) {
- if(hit.collider.gameObject == touchArea){
- Vector3 mpos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
- touchRepeatDic[c] = touchArea.name;
- coroutine = ChangeColorOfGameObject (touchArea.gameObject, red ,false);
- StartCoroutine ( coroutine);
- drawTouchedLine( mpos);
- }
- }else{
- touchPos = Vector2.zero;
- _renderer.enabled = false;
- }
- //Debug.Log (touchPos);
- }
- else{
- touchPos = Vector2.zero;
- _renderer.enabled = false;
- coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- }
- }
- #endif
- }
- private Vector3 LowPassFilterAccelerometer() {
- acceleration = Vector3.Lerp(acceleration, Input.acceleration, LowPassFilterFactor);
- return acceleration;
- }
- private void drawTouchedLine(Vector3 mpos){
- // -0.5 < xpos(ypos) < 0.5
- touchPos.x = (mpos.x - touchArea.transform.position.x) / touchArea.transform.lossyScale.x;
- touchPos.y = (mpos.y - touchArea.transform.position.y) / touchArea.transform.lossyScale.y;
- _renderer.enabled = true;
- _renderer.SetVertexCount(6);
- _renderer.SetColors (red, red);
- // 頂点を設定
- _renderer.SetPosition(0, new Vector3(mpos.x,
- touchArea.transform.position.y+(touchArea.transform.localScale.y/2),
- -2.0f));
- //renderer.SetPosition(1, new Vector3(mpos.x,touchArea.transform.lossyScale.y,-2.0f));
- _renderer.SetPosition(1, new Vector3(mpos.x,
- touchArea.transform.position.y-(touchArea.transform.localScale.y/2),
- -2.0f));
- _renderer.SetPosition(2, new Vector3(mpos.x,
- touchArea.transform.position.y-(touchArea.transform.localScale.y/2),
- -20.0f));
- _renderer.SetPosition(3, new Vector3(touchArea.transform.position.x-(touchArea.transform.localScale.x/2),
- mpos.y,
- -20.0f));
- // 頂点を設定
- _renderer.SetPosition(4, new Vector3(touchArea.transform.position.x-(touchArea.transform.localScale.x/2),
- mpos.y,
- -2.0f));
- _renderer.SetPosition(5, new Vector3(touchArea.transform.position.x+(touchArea.transform.localScale.x/2),
- mpos.y,
- -2.0f));
- }
- /// <summary>
- /// 入力されたオブジェクト及びその子、全ての色を変える
- /// </summary>
- /// <param name="targetObject">色を変更したいオブジェクト</param>
- /// <param name="color">設定したい色</param>
- ///
- private IEnumerator ChangeColorOfGameObject(GameObject targetObject, Color color,bool one = true){
- //入力されたオブジェクトのRendererを全て取得し、さらにそのRendererに設定されている全Materialの色を変える
- foreach(Renderer targetRenderer in targetObject.GetComponents<Renderer>()){
- foreach(Material material in targetRenderer.materials){
- material.color = color;
- if(one){
- yield return new WaitForSeconds (0.2f);
- // Prints 5.0
- material.color = originColor;
- }
- }
- }
- //入力されたオブジェクトの子にも同様の処理を行う
- for(int i = 0; i < targetObject.transform.childCount; i++){
- coroutine = ChangeColorOfGameObject (targetObject.transform.GetChild(i).gameObject, color);
- StartCoroutine ( coroutine);
- }
- }
- public void changeSensorType(xInputType type){
- this.sensorType = type;
- if (type == xInputType.OFF) {
- touchpadObj.SetActive (false);
- //renderer.enabled = false;
- gamepadObj.SetActive (false);
- //_renderer.enabled = false;
- Screen.orientation = ScreenOrientation.Portrait;
- } else if (type == xInputType.TOUCHPAD) {
- touchpadObj.SetActive (true);
- gamepadObj.SetActive (false);
- _renderer.enabled = false;
- coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- Screen.orientation = ScreenOrientation.Portrait;
- } else if (type == xInputType.GAMEPAD) {
- touchpadObj.SetActive(false);
- gamepadObj.SetActive(true);
- _renderer.enabled = false;
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- }
- else {
- touchpadObj.SetActive (true);
- gamepadObj.SetActive (false);
- _renderer.enabled = false;
- coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
- StartCoroutine ( coroutine);
- Screen.orientation = ScreenOrientation.Portrait;
- }
- }
- void OnGUI () {
- // Make a background box
- GUI.Box(new Rect(Screen.width-140,Screen.height-20,140,20), "TYPE:"+sensorType.ToString()) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement