@@ -30,11 +30,10 @@ public class GraphCreator : MonoBehaviour {
3030 [ SerializeField ] float sizeStepY = 50 ;
3131
3232 [ Header ( "Количество шагов по оси Х / Number of steps along the axis X" ) ]
33- [ SerializeField ]
34- int Xsteps ;
33+ [ SerializeField ] int Xsteps ;
34+
3535 [ Header ( "Количество шагов по оси Y/ Number of steps along the axis Y" ) ]
36- [ SerializeField ]
37- int Ysteps ;
36+ [ SerializeField ] int Ysteps ;
3837
3938 public List < Vector2 > dataPoints = new List < Vector2 > ( ) ;
4039 RectTransform mainTransform ;
@@ -46,14 +45,15 @@ public class GraphCreator : MonoBehaviour {
4645 [ Header ( "стартовая координата по оси У / Y-axis origin" ) ]
4746 [ SerializeField ]
4847 float StartCoordinateY = 0 ;
49- protected float step = 0 ;
50-
48+
5149 public float valueX { private get ; set ; }
5250 public float valueY { private get ; set ; }
51+
5352 protected Vector2 lastPoint ;
53+ protected float step = 0 ;
5454
5555 private void Awake ( ) {
56- mainTransform = GetComponent < RectTransform > ( ) ;
56+ mainTransform = Graph . GetComponent < RectTransform > ( ) ;
5757 PointForUTransformTmp = PointForUTransform ;
5858 }
5959
@@ -71,12 +71,12 @@ protected void AddTextForX() {
7171 if ( sizeStepX > 0 ) {
7272 step = sizeStepX / Xsteps ;
7373 for ( float i = StartCoordinateX ; i <= sizeStepX ; i += step ) {
74- addTestForX ( i ) ;
74+ addTestForXinTheLoop ( i ) ;
7575 }
7676 } else {
7777 step = Math . Abs ( sizeStepX / Xsteps ) ;
7878 for ( float i = StartCoordinateX ; i >= sizeStepX ; i -= step ) {
79- addTestForX ( i , true ) ;
79+ addTestForXinTheLoop ( i ) ;
8080 }
8181 }
8282 axisTextHolder . GetChild ( 0 ) . gameObject . SetActive ( false ) ;
@@ -87,54 +87,33 @@ protected void AddTextForY() {
8787 if ( sizeStepY > 0 ) {
8888 step = sizeStepY / Ysteps ;
8989 for ( float i = StartCoordinateY ; i <= sizeStepY ; i += step ) {
90- addTestForY ( i ) ;
90+ addTestForYinTheLoop ( i ) ;
9191 }
9292 } else {
9393 step = Math . Abs ( sizeStepY / Ysteps ) ;
9494 for ( float i = StartCoordinateY ; i >= sizeStepY ; i -= step ) {
95- addTestForY ( i , true ) ;
95+ addTestForYinTheLoop ( i ) ;
9696 }
9797 }
9898 axisTextHolder . GetChild ( 1 ) . gameObject . SetActive ( false ) ;
9999 }
100100
101- protected void addTestForX ( float i , bool negative = false ) {
101+ protected void addTestForXinTheLoop ( float i ) {
102102 GameObject newText = Instantiate ( axisTextHolder . GetChild ( 0 ) . gameObject , axisTextHolder ) ;
103- newText . GetComponent < RectTransform > ( ) . anchoredPosition = new Vector2 ( ( i * mainTransform . sizeDelta . x ) / sizeStepX , 0f ) ;
104- if ( ! negative ) newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( i , 1 ) . ToString ( ) ;
105- else newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( sizeStepX - i , 1 ) . ToString ( ) ;
103+ newText . GetComponent < RectTransform > ( ) . anchoredPosition = new Vector2 ( ( i * mainTransform . sizeDelta . x ) / sizeStepX , 0f ) ;
104+ newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( i , 1 ) . ToString ( ) ;
106105 newText . transform . GetChild ( 0 ) . GetComponent < RectTransform > ( ) . sizeDelta = new Vector2 ( 1 , mainTransform . sizeDelta . y ) ;
107106 if ( i == 0f ) newText . transform . GetChild ( 0 ) . gameObject . SetActive ( false ) ;
108107 }
109108
110- protected void addTestForY ( float i , bool negative = false ) {
109+ protected void addTestForYinTheLoop ( float i ) {
111110 GameObject newText = Instantiate ( axisTextHolder . GetChild ( 1 ) . gameObject , axisTextHolder ) ;
112111 newText . GetComponent < RectTransform > ( ) . anchoredPosition = new Vector2 ( 0f , ( i * mainTransform . sizeDelta . y ) / sizeStepY ) ;
113- if ( i > 0f )
114- if ( ! negative ) newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( i , 1 ) . ToString ( ) + " " ;
115- else newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( sizeStepY - 1 , 1 ) . ToString ( ) + " " ;
112+ if ( i > 0f ) newText . GetComponent < TextMeshProUGUI > ( ) . text = Math . Round ( i , 1 ) . ToString ( ) ;
116113 newText . transform . GetChild ( 0 ) . GetComponent < RectTransform > ( ) . sizeDelta = new Vector2 ( mainTransform . sizeDelta . x , 1 ) ;
117114 if ( i == 0f ) newText . transform . GetChild ( 0 ) . gameObject . SetActive ( false ) ;
118115 }
119116
120- public void DeleteAllPoints ( ) {
121- if ( InstancePoints . Count != 0 ) {
122- DeleteEveryGameObj ( InstancePoints ) ;
123- LineRenderForCreateGraph . Points = new Vector2 [ 0 ] ;
124- PointForUTransform = PointForUTransformTmp ;
125- lastPoint = Vector2 . zero ;
126- dataPoints . Clear ( ) ;
127- }
128- }
129-
130- protected void DeleteEveryGameObj ( List < GameObject > DeleteGameObj ) {
131- if ( DeleteGameObj . Count != 0 ) {
132- for ( int i = DeleteGameObj . Count - 1 ; i >= 0 ; i -- )
133- Destroy ( DeleteGameObj [ i ] ) ;
134- DeleteGameObj . Clear ( ) ;
135- }
136- }
137-
138117 public void AddPoint ( ) {
139118 if ( ValidateAddingPoint ( ) ) {
140119 if ( lastPoint == new Vector2 ( valueX , valueY ) ) return ;
@@ -160,6 +139,11 @@ public void AddPoint() {
160139 }
161140 }
162141
142+ protected bool ValidateAddingPoint ( ) {
143+ if ( Math . Abs ( valueX ) > Math . Abs ( sizeStepX ) || Math . Abs ( valueY ) > Math . Abs ( sizeStepY ) ) return false ;
144+ return true ;
145+ }
146+
163147 public void DeletePoint ( ) {
164148 if ( LineRenderForCreateGraph . Points . Length == 0 ) return ;
165149 Vector2 [ ] newPoints = new Vector2 [ LineRenderForCreateGraph . Points . Length - 1 ] ;
@@ -171,9 +155,23 @@ public void DeletePoint() {
171155 dataPoints . RemoveAt ( 0 ) ;
172156 }
173157
174- protected bool ValidateAddingPoint ( ) {
175- if ( Math . Abs ( valueX ) > Math . Abs ( sizeStepX ) || Math . Abs ( valueY ) > Math . Abs ( sizeStepY ) ) return false ;
176- return true ;
158+ public void DeleteAllPoints ( ) {
159+ if ( InstancePoints . Count != 0 ) {
160+ DeleteEveryGameObj ( InstancePoints ) ;
161+ LineRenderForCreateGraph . Points = new Vector2 [ 0 ] ;
162+ PointForUTransform = PointForUTransformTmp ;
163+ lastPoint = Vector2 . zero ;
164+ dataPoints . Clear ( ) ;
165+ }
166+ }
167+
168+ protected void DeleteEveryGameObj ( List < GameObject > DeleteGameObj ) {
169+ if ( DeleteGameObj . Count != 0 ) {
170+ for ( int i = DeleteGameObj . Count - 1 ; i >= 0 ; i -- )
171+ Destroy ( DeleteGameObj [ i ] ) ;
172+ DeleteGameObj . Clear ( ) ;
173+ }
177174 }
175+
178176}
179177
0 commit comments