UnityMol  0.9.6-875
UnityMol viewer / In developement
MeasureDistance.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 
69 public class MeasureDistance : MonoBehaviour
70 {
71  LineRenderer mLine;
72  public GameObject obj1;
73  public GameObject obj2;
74  public GameObject lineObject;
75 
76  private LineRenderer line;
77  private float objDistance;
78  public bool isChange = false;
79  private float coordinate_x=-100;
80  private float coordinate_y=-100;
81  //private float coordinate_x2=0;
82  //private float coordinate_y2=0;
83  //private string atominfo="";
84 
85  private GameObject halo1;
86  private GameObject halo2;
87  private Camera mainCamera = null;
88 
89  void Start(){
90  mainCamera = Camera.main;
91  }
92 
93 
94  void initLine()
95  {
96  lineObject = new GameObject("measureline");
97  line = lineObject.AddComponent<LineRenderer>();
98  line.material = new Material (Shader.Find("Particles/Alpha Blended"));
99  line.SetColors(Color.red, Color.red);
100  line.SetWidth(0.3f, 0.3f);
101 // lineObject.active = false;
102  lineObject.SetActive(false);
103  objDistance = -1f;
104  }
105 
106 
107  void OnEnable()
108  {
109  isChange = false;
110  obj1 = null;
111  obj2 = null;
112  initLine();
113  }
114 
115  void Update ()
116  {
117  if(mainCamera == null)
118  mainCamera = Camera.main;
119  if (Input.GetButtonDown ("Fire1"))
120  {
121  Ray sRay= mainCamera.ScreenPointToRay (Input.mousePosition);
122  RaycastHit sHit;
123  if (Physics.Raycast(sRay, out sHit))
124  {
125  if (isChange)
126  {
127  obj2 = sHit.collider.gameObject;
128 
129  line.SetPosition(1, obj2.transform.position);
130  line.name="measureline";
131 // lineObject.active = true;
132  lineObject.SetActive(true);
133 
134  isChange = false;
135  objDistance = Vector3.Distance(obj1.transform.position, obj2.transform.position);
136 
137  halo2=(GameObject)Instantiate(Resources.Load("transparentsphere"),
138  obj2.transform.localPosition,
139  new Quaternion(0f,0f,0f,0f));
140 
141  float rad = 0f;
142  if(UI.UIData.atomtype == UI.UIData.AtomType.optihb)
143  rad =obj2.GetComponent<SphereCollider>().radius*2;
144  else
145  rad = obj2.GetComponent<BallUpdate>().GetRealRadius();
146 
147  halo2.transform.localScale = new Vector3(rad+1,rad+1,rad+1);
148  }
149  else
150  {
151  Destroy(halo1);
152  Destroy(halo2);
153 
154  obj1 = sHit.collider.gameObject;
155 
156  line.SetPosition(0, obj1.transform.position);
157 // lineObject.active = false;
158  lineObject.SetActive(false);
159 
160  isChange = true;
161  objDistance=-1;
162 
163  coordinate_x= Input.mousePosition.x + 5;
164  coordinate_y= Screen.height-Input.mousePosition.y - 20;
165 // coordinate_x2= Input.mousePosition.x ;
166 // coordinate_y2= Screen.height-Input.mousePosition.y ;
167 
168  halo1=(GameObject)Instantiate(Resources.Load("transparentsphere"),
169  obj1.transform.localPosition,
170  new Quaternion(0f,0f,0f,0f));
171 
172  float rad = 0f;
173  if(UI.UIData.atomtype == UI.UIData.AtomType.optihb)
174  rad =obj1.GetComponent<SphereCollider>().radius*2;
175  else
176  rad = obj1.GetComponent<BallUpdate>().GetRealRadius();
177 
178 
179  halo1.transform.localScale = new Vector3(rad+1,rad+1,rad+1);
180  }
181  }
182 
183  }
184 
185 
186  if (Input.GetMouseButtonDown(1))
187  {
188 // lineObject.active = false;
189  lineObject.SetActive(false);
190  objDistance = -1;
191  Destroy(halo1);
192  Destroy(halo2);
193  }
194  }
195 
196  void OnGUI()
197  {
198  if(objDistance > -1)
199  GUI.Box(new Rect (coordinate_x,coordinate_y, 100, 20),objDistance.ToString());
200  }
201 
202  void OnDisable()
203  {
204  Destroy(halo1);
205  Destroy(halo2);
206  Destroy(lineObject);
207  }
208 
209 }
GameObject halo1
GameObject halo2
GameObject lineObject
LineRenderer line
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static AtomType atomtype
Definition: UIData.cs:139
LineRenderer mLine
Definition: GUIDisplay.cs:66