Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5.  
  6. public class DeviceSensors : MonoBehaviour {
  7.  
  8.  
  9.  
  10.  
  11. public Vector3 acceleration; //Input.acceleration
  12. public Vector3 gyroUnbiased;
  13. public Quaternion gyroAttitude;
  14.  
  15. public Dictionary<int,string> touchRepeatDic;
  16.  
  17. private IEnumerator coroutine;
  18. private Color red = Color.red;
  19. private Color originColor;
  20.  
  21. public xInputType sensorType = xInputType.TOUCHPAD;
  22.  
  23. public GameObject touchArea;
  24. public Vector2 touchPos= Vector2.zero;
  25.  
  26. private GameObject gamepadObj;
  27. private GameObject touchpadObj;
  28.  
  29. public LineRenderer _renderer;// = gameObject.GetComponent<LineRenderer>();
  30.  
  31.  
  32.  
  33. //low pass fliler
  34. static float AccelerometerUpdateInterval = 1.0f / 60.0f;
  35. static float LowPassKernelWidthInSeconds= 1.0f;
  36. private float LowPassFilterFactor =AccelerometerUpdateInterval / LowPassKernelWidthInSeconds; // tweakable
  37. //private Vector3 lowPassValue = Vector3.zero;
  38.  
  39. // Use this for initialization
  40. void Start () {
  41.  
  42.  
  43.  
  44. #if UNITY_EDITOR
  45.  
  46. Resolution.Instance.SetScreenWidthAndHeightFromEditorGameViewViaReflection ();
  47. float w = (float)Resolution.Instance.ScreenWidth;
  48. float h = (float)Resolution.Instance.ScreenHeight;
  49.  
  50. #else
  51. float w = Screen.width;
  52. float h = Screen.height;
  53. #endif
  54.  
  55.  
  56. float s = (w > h ? h:w )/ 5.625f;//192.0f;
  57.  
  58.  
  59.  
  60. float size = w / ((( w / h ) * 2 ) * s );//5.625f;//
  61.  
  62.  
  63. //Debug.Log ("調整" + (float)Resolution.Instance.ScreenWidth / (float)Resolution.Instance.ScreenHeight);
  64. if (w/ h > 0.60f) {
  65. size *= 1.2f;
  66.  
  67. }
  68.  
  69. Debug.Log ("Start DeviceSensors w:" +w + "/h:" + h + "/s:" + s + "/size:" + size) ;
  70.  
  71.  
  72. //タブレット端末など
  73.  
  74.  
  75. //float s = Screen.height / 5.625f;//192.0f;
  76.  
  77. //float size = h / ((( h / w ) * 2 ) * s );
  78.  
  79. Camera.main.orthographicSize = size;
  80.  
  81. red = ColorUtils.ToRGB (0xC83FB3);
  82.  
  83. originColor = GameObject.Find ("ButtonA").transform.GetComponents<Renderer> () [0].materials [0].color;
  84.  
  85.  
  86. touchRepeatDic = new Dictionary<int,string>();
  87.  
  88.  
  89. acceleration = Input.acceleration;
  90.  
  91.  
  92. gyroUnbiased = Input.gyro.rotationRateUnbiased;
  93.  
  94. if (SystemInfo.supportsGyroscope) {
  95. Input.gyro.enabled = true;
  96. } else {
  97. Debug.Log ("no support gyro.");
  98. }
  99.  
  100. //Compass
  101. Input.compass.enabled = true;
  102.  
  103.  
  104. gamepadObj = GameObject.Find ("GamePad");
  105. touchpadObj= GameObject.Find ("TouchPad");
  106.  
  107. changeSensorType (sensorType);
  108.  
  109. _renderer= gameObject.GetComponent<LineRenderer>();
  110. // 線の幅
  111. _renderer.SetWidth(0.1f, 0.1f);
  112. // 頂点の数
  113.  
  114. }
  115.  
  116. // Update is called once per frame
  117. void Update () {
  118.  
  119. if(sensorType == xInputType.OFF)
  120. return;
  121.  
  122. // get acceleration
  123. //acceleration = Input.acceleration;
  124. acceleration = LowPassFilterAccelerometer ();
  125.  
  126. gyroUnbiased = Input.gyro.rotationRateUnbiased;
  127.  
  128.  
  129. // Debug.Log ("acceleration" + dir);
  130.  
  131. // get gyro attitude
  132. if (Input.gyro.enabled) {
  133. gyroAttitude = Input.gyro.attitude;
  134. /*
  135. gyroAttitude.x *= -1;
  136. gyroAttitude.y *= -1;
  137. gyroAttitude.z *= 1;
  138. */
  139.  
  140. }
  141.  
  142. // get Conpass
  143. transform.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);
  144.  
  145.  
  146.  
  147.  
  148.  
  149. #if ( UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8 ) && UNITY_EDITOR
  150.  
  151. /* Touch Controlls*/
  152. Touch[] _touch = Input.touches;
  153.  
  154.  
  155. if(_touch.Length == 0 ){
  156.  
  157. touchPos = Vector2.zero;
  158. _renderer.enabled = false;
  159.  
  160. if( touchRepeatDic.Count > 0){
  161.  
  162. Debug.Log("FORCE RESET ");
  163.  
  164. foreach(var d in touchRepeatDic){
  165. coroutine = ChangeColorOfGameObject (GameObject.Find(d.Value).gameObject, originColor ,false);
  166. StartCoroutine ( coroutine);
  167.  
  168. touchRepeatDic.Remove(d.Key);
  169.  
  170. }
  171. }
  172.  
  173. }
  174. else{
  175. foreach(var touch in _touch){
  176.  
  177. //changed
  178. if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Began){
  179. Ray ray = Camera.main.ScreenPointToRay (touch.position);
  180. RaycastHit hit = new RaycastHit ();
  181.  
  182. if (Physics.Raycast (ray, out hit)) {
  183.  
  184. GameObject obj = hit.collider.gameObject;
  185.  
  186.  
  187. if(obj == touchArea ){
  188. drawTouchedLine(Camera.main.ScreenToWorldPoint(touch.position) );
  189. }
  190.  
  191. SendMessage("OscSendTouchBegin", obj.name);
  192.  
  193. touchRepeatDic[touch.fingerId] = obj.name;
  194. coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
  195. StartCoroutine ( coroutine);
  196.  
  197. }
  198.  
  199. }
  200.  
  201. if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Moved){
  202.  
  203. Ray ray = Camera.main.ScreenPointToRay (touch.position);
  204. RaycastHit hit = new RaycastHit ();
  205.  
  206. if (Physics.Raycast (ray, out hit)) {
  207.  
  208. GameObject obj = hit.collider.gameObject;
  209.  
  210. if(obj == touchArea ){
  211. drawTouchedLine(Camera.main.ScreenToWorldPoint(touch.position) );
  212.  
  213. }
  214.  
  215. if(touchRepeatDic.ContainsKey(touch.fingerId) == false){
  216. // Debug.Log ("KEYSTART"+ obj.name);
  217. SendMessage("OscSendTouchBegin", obj.name);
  218.  
  219. touchRepeatDic[touch.fingerId] = obj.name;
  220. }
  221.  
  222. //slide another button
  223. else if(touchRepeatDic[touch.fingerId] != obj.name ){
  224.  
  225. coroutine = ChangeColorOfGameObject (GameObject.Find(touchRepeatDic[touch.fingerId]).gameObject, originColor ,false);
  226. StartCoroutine ( coroutine);
  227. SendMessage("OscSendTouchEnd", touchRepeatDic[touch.fingerId]);
  228. //touchState[Input.GetTouch(i).fingerId] = "";
  229. touchRepeatDic.Remove(touch.fingerId);
  230.  
  231. }
  232.  
  233. touchRepeatDic[touch.fingerId] = obj.name;
  234. coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
  235. StartCoroutine ( coroutine);
  236. }
  237.  
  238.  
  239. }
  240.  
  241. if (Input.GetTouch(touch.fingerId).phase == TouchPhase.Ended || Input.GetTouch(touch.fingerId).phase == TouchPhase.Canceled ){
  242.  
  243. Debug.Log("END1:" +touch.fingerId );
  244.  
  245. if(touchRepeatDic[touch.fingerId] != null ){
  246. Debug.Log("END2:" +touch.fingerId + ":" + touchRepeatDic[touch.fingerId] );
  247. if(touchRepeatDic[touch.fingerId] == touchArea.name ){
  248. touchPos = Vector2.zero;
  249. _renderer.enabled = false;
  250. }
  251.  
  252. SendMessage("OscSendTouchEnd", touchRepeatDic[touch.fingerId]);
  253. coroutine = ChangeColorOfGameObject (GameObject.Find(touchRepeatDic[touch.fingerId]).gameObject, originColor ,false);
  254. StartCoroutine ( coroutine);
  255. touchRepeatDic.Remove(touch.fingerId);
  256.  
  257.  
  258. }
  259.  
  260. }
  261.  
  262. }
  263.  
  264. }
  265.  
  266. #else
  267.  
  268. /* Keyboard Controlls*/
  269. int c =0;
  270.  
  271.  
  272.  
  273. foreach (var k in xMultiInputs.xKeymap.Keys){
  274.  
  275. if(Input.GetKeyDown(k)){ //keydown
  276. try{
  277. GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
  278. SendMessage("OscSendTouchBegin", obj.name);
  279. coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
  280. StartCoroutine ( coroutine);
  281. }catch(UnityException e){}
  282.  
  283.  
  284.  
  285.  
  286.  
  287. }
  288. else if(Input.GetKeyUp(k)){ //keyup
  289. try{
  290. GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
  291.  
  292. coroutine = ChangeColorOfGameObject (obj.gameObject, originColor ,false);
  293. StartCoroutine ( coroutine);
  294.  
  295. SendMessage("OscSendTouchEnd", obj.name);
  296. }catch(UnityException e){}
  297. }
  298. else if(Input.GetKey (k)){ //repeat
  299.  
  300. //Debug.Log (xMultiInputs.xKeymap[k]);
  301. try{
  302. GameObject obj = GameObject.Find(xMultiInputs.xKeymap[k].name).gameObject;
  303. touchRepeatDic[c] = obj.name;
  304. coroutine = ChangeColorOfGameObject (obj.gameObject, red ,false);
  305. StartCoroutine ( coroutine);
  306. c++;
  307. }catch(UnityException e){
  308. }
  309.  
  310. }
  311.  
  312. }
  313.  
  314.  
  315. //delete previous repeat dict.
  316. for (int s = touchRepeatDic.Count - 1; s >= c; s--) {
  317. touchRepeatDic.Remove(s);
  318. }
  319.  
  320. if(sensorType == xInputType.TOUCHPAD){
  321.  
  322.  
  323.  
  324. if(Input.GetMouseButtonUp(0)){
  325. touchPos = Vector2.zero;
  326. _renderer.enabled = false;
  327.  
  328. SendMessage("OscSendTouchEnd", touchArea.name);
  329.  
  330. }
  331. else if(Input.GetMouseButtonDown(0)){
  332. SendMessage("OscSendTouchBegin", touchArea.name);
  333. }
  334. else if(Input.GetMouseButton(0)){
  335. Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  336. RaycastHit hit = new RaycastHit ();
  337.  
  338. if (Physics.Raycast (ray, out hit)) {
  339.  
  340. if(hit.collider.gameObject == touchArea){
  341. Vector3 mpos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
  342.  
  343. touchRepeatDic[c] = touchArea.name;
  344. coroutine = ChangeColorOfGameObject (touchArea.gameObject, red ,false);
  345. StartCoroutine ( coroutine);
  346.  
  347. drawTouchedLine( mpos);
  348. }
  349. }else{
  350. touchPos = Vector2.zero;
  351. _renderer.enabled = false;
  352. }
  353.  
  354. //Debug.Log (touchPos);
  355.  
  356. }
  357. else{
  358. touchPos = Vector2.zero;
  359. _renderer.enabled = false;
  360. coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
  361. StartCoroutine ( coroutine);
  362.  
  363.  
  364. }
  365. }
  366.  
  367.  
  368. #endif
  369.  
  370. }
  371.  
  372. private Vector3 LowPassFilterAccelerometer() {
  373. acceleration = Vector3.Lerp(acceleration, Input.acceleration, LowPassFilterFactor);
  374.  
  375. return acceleration;
  376. }
  377.  
  378. private void drawTouchedLine(Vector3 mpos){
  379.  
  380.  
  381.  
  382. // -0.5 < xpos(ypos) < 0.5
  383. touchPos.x = (mpos.x - touchArea.transform.position.x) / touchArea.transform.lossyScale.x;
  384. touchPos.y = (mpos.y - touchArea.transform.position.y) / touchArea.transform.lossyScale.y;
  385.  
  386.  
  387. _renderer.enabled = true;
  388. _renderer.SetVertexCount(6);
  389. _renderer.SetColors (red, red);
  390.  
  391.  
  392. // 頂点を設定
  393. _renderer.SetPosition(0, new Vector3(mpos.x,
  394. touchArea.transform.position.y+(touchArea.transform.localScale.y/2),
  395. -2.0f));
  396.  
  397. //renderer.SetPosition(1, new Vector3(mpos.x,touchArea.transform.lossyScale.y,-2.0f));
  398. _renderer.SetPosition(1, new Vector3(mpos.x,
  399. touchArea.transform.position.y-(touchArea.transform.localScale.y/2),
  400. -2.0f));
  401.  
  402. _renderer.SetPosition(2, new Vector3(mpos.x,
  403. touchArea.transform.position.y-(touchArea.transform.localScale.y/2),
  404. -20.0f));
  405. _renderer.SetPosition(3, new Vector3(touchArea.transform.position.x-(touchArea.transform.localScale.x/2),
  406. mpos.y,
  407. -20.0f));
  408.  
  409.  
  410. // 頂点を設定
  411. _renderer.SetPosition(4, new Vector3(touchArea.transform.position.x-(touchArea.transform.localScale.x/2),
  412. mpos.y,
  413. -2.0f));
  414. _renderer.SetPosition(5, new Vector3(touchArea.transform.position.x+(touchArea.transform.localScale.x/2),
  415. mpos.y,
  416. -2.0f));
  417. }
  418.  
  419.  
  420.  
  421. /// <summary>
  422. /// 入力されたオブジェクト及びその子、全ての色を変える
  423. /// </summary>
  424. /// <param name="targetObject">色を変更したいオブジェクト</param>
  425. /// <param name="color">設定したい色</param>
  426. ///
  427. private IEnumerator ChangeColorOfGameObject(GameObject targetObject, Color color,bool one = true){
  428.  
  429.  
  430.  
  431.  
  432. //入力されたオブジェクトのRendererを全て取得し、さらにそのRendererに設定されている全Materialの色を変える
  433. foreach(Renderer targetRenderer in targetObject.GetComponents<Renderer>()){
  434. foreach(Material material in targetRenderer.materials){
  435.  
  436. material.color = color;
  437. if(one){
  438. yield return new WaitForSeconds (0.2f);
  439. // Prints 5.0
  440. material.color = originColor;
  441.  
  442. }
  443. }
  444. }
  445.  
  446. //入力されたオブジェクトの子にも同様の処理を行う
  447. for(int i = 0; i < targetObject.transform.childCount; i++){
  448. coroutine = ChangeColorOfGameObject (targetObject.transform.GetChild(i).gameObject, color);
  449. StartCoroutine ( coroutine);
  450.  
  451. }
  452.  
  453. }
  454.  
  455.  
  456. public void changeSensorType(xInputType type){
  457.  
  458.  
  459.  
  460. this.sensorType = type;
  461. if (type == xInputType.OFF) {
  462.  
  463.  
  464. touchpadObj.SetActive (false);
  465. //renderer.enabled = false;
  466. gamepadObj.SetActive (false);
  467.  
  468. //_renderer.enabled = false;
  469.  
  470. Screen.orientation = ScreenOrientation.Portrait;
  471.  
  472.  
  473.  
  474. } else if (type == xInputType.TOUCHPAD) {
  475.  
  476.  
  477.  
  478. touchpadObj.SetActive (true);
  479. gamepadObj.SetActive (false);
  480.  
  481. _renderer.enabled = false;
  482. coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
  483. StartCoroutine ( coroutine);
  484.  
  485. Screen.orientation = ScreenOrientation.Portrait;
  486.  
  487.  
  488. } else if (type == xInputType.GAMEPAD) {
  489.  
  490.  
  491.  
  492. touchpadObj.SetActive(false);
  493. gamepadObj.SetActive(true);
  494. _renderer.enabled = false;
  495. Screen.orientation = ScreenOrientation.LandscapeLeft;
  496.  
  497. }
  498.  
  499. else {
  500.  
  501.  
  502. touchpadObj.SetActive (true);
  503. gamepadObj.SetActive (false);
  504.  
  505. _renderer.enabled = false;
  506. coroutine = ChangeColorOfGameObject (touchArea.gameObject, originColor ,false);
  507. StartCoroutine ( coroutine);
  508. Screen.orientation = ScreenOrientation.Portrait;
  509.  
  510.  
  511. }
  512.  
  513.  
  514.  
  515. }
  516.  
  517. void OnGUI () {
  518.  
  519. // Make a background box
  520. GUI.Box(new Rect(Screen.width-140,Screen.height-20,140,20), "TYPE:"+sensorType.ToString()) ;
  521.  
  522. }
  523.  
  524. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement