반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Main
{
 
    void Chk_number( int n)
    {
        if(n % 2 == 0 &&  n != 0//짝수 임
        {
        }
        else if(n % 2 == 1// 홀수임
        {
        }
        else // 0 입니다.
        {
        }    
 
    }
 
 
}
 
cs



특정수를 2로 나누워서 나머지가 0 이면 짝수,

특정수를 2로 나누워서 나머지가 1이면 홀수 입니다.



이상입니다. 

반응형
반응형







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public RectTransform viewPortRectTr; // 보는 곳 RectTranform ex) scrollview 의 viewport
public RectTransform contentRectTr; // 리스트 들들어잇는 content transform
 
 
 
// 가변적으로 변하는 content trasform 에 따라 viewPortRectTr 로컬기준으로 Bounds를 구할수 있다.
private readonly Vector3[] m_Corners = new Vector3[4];
 
    private Bounds GetContentBounds()
    {
        if (contentRectTr == null)
            return new Bounds();
 
        var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
 
        var toLocal = viewPortRectTr.worldToLocalMatrix; // viewPortRectTr의 로컬 매트릭스 가져오기
        contentRectTr.GetWorldCorners(m_Corners); //  contentRectTr 네 각 꼭지점의 vector3
        for (int j = 0; j < 4; j++)
        {
            Vector3 v = toLocal.MultiplyPoint3x4(m_Corners[j]); // viewPortRectTr의 로컬 매트릭스 * vector3 = 각 꼭지점의 contentRectTr 로컬좌표
            vMin = Vector3.Min(v, vMin);
            vMax = Vector3.Max(v, vMax);
        }
 
        // 제일 작은 수로 센터로  크기가 0 인 바운드만들고
        var bounds = new Bounds(vMin, Vector3.zero);
        //제일 큰값 으로 바운드 확
        bounds.Encapsulate(vMax);
        return bounds;
    }
cs


반응형
반응형
1
2
3
4
5
6
7
8
9
10
[Flags]
public enum MonsterType
{
    None = 0,
    Human = 1 << 0,
    Flyer = 1 << 1,
    Machine = 1 << 2,
    Fish = 1 << 3,
    All = int.MaxValue
};
cs



먼저 [Flags] 기능 은

이를 이용하면 여러 속성을 선택하는 것이 가능합니다.
 코드는 열거형의 예시로 정리한것입니다.




다음은 Flag 어트리뷰트 된 enum이 여러 속성을 선택할수 있게 연산하는 예입니다.


연산을 하게 되면 . 열거 값을 유지하면서 열거 값을 결합 할 수 있습니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[Flags]
public enum MonsterType
{
    None = 0,
    Human = 1 << 0,
    Flyer = 1 << 1,
    Machine = 1 << 2,
    Fish = 1 << 3,
    All = int.MaxValue
};
 
 
MonsterType MyType = MonsterType.Human | MonsterType.Flyer ;
 
//값이 존재하는지 확인:
if((MyType & MonsterType.Human ) != 0
 
//특정 값을 추가:
MyType |= MonsterType.Machine ;
 
//특정 값을 제거:
MyType &= ~MonsterType.Machine ;
 
//특정 값을 반전(1은 0으로, 0은 1로):
MyType ^= MonsterType.Contract;
 
//모든 값 삭제:
MyType = MonsterType.None;
 
//모든 값 설정:
MyType = MonsterType.All;
 
//특정 값을 제외하고 모두 설정:
MyType = MonsterType.All ^ MonsterType.Human ^ MonsterType.Machine ;
 
 
cs


반응형
반응형
예시 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//델리게이트 전역 선언
public delegate void Next_Process();
 
 
 
public class Main: MonoBehaviour
{
 
    public LoadManager _LoadManager;
 
    public void ChangeScene()
    {
        _LoadManager.LoadScene("Battle", Callbakc_SceneLoadMidProcess, Callback_SceneLoadComplete);
    }
 
    void Callbakc_SceneLoadMidProcess()
    {
        debug.Log("Mid process");
    }
 
    void Callback_SceneLoadComplete()
    {
        debug.Log("complete process");
    }
 
}
 
 
 
 
 
 
 
 
 
public class LoadManager : MonoBehaviour 
{
 
    public void LoadScene(string sceneName,Next_Process midProcess, Next_Process completeProcess)
    {
        StartCoroutine(routine_LoadScene(sceneName, midProcess, completeProcess));
    }
 
    IEnumerator routine_LoadScene(string sceneName,Next_Process midProcess, Next_Process completeProcess)
    {
        asyncLoadScene = SceneManager.LoadSceneAsync(sceneName);
 
        asyncLoadScene.allowSceneActivation = false;
 
            while (true)
            {
                if (asyncLoadScene.isDone)
                {
                    Debug.Log("DONE");
                    
                    //완료시 콜백
                    if (completeProcess != null)
                        completeProcess();
                        break;
                }
    
                if (asyncLoadScene.progress == 0.9f)
                {
                    Debug.Log("mid");
                    //90퍼시 진행시 콜백
                    if(midProcess != null)
                        midProcess();
 
                }
 
            
 
                Debug.Log(asyncLoadScene.progress + " % ");
 
                yield return null;
            }
        
    }
 
}
cs












예시예시2

1


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
//델리게이트 전역선언
public delegate void Next_Process(); // void형 델리게이트
public delegate void Next_AttackProcess(RaycastHit _hitInfo); //RaycastHit 형 델리게이트
 
public class AttackComponent : ComponentBase    
{
    private Next_AttackProcess SuccessAttack;
    private Next_Process FailAttack;
 
    //생성자
    public AttackComponent(UnitBase _uInfo, string _attackTagName, Next_AttackProcess _SuccessAttack = null, Next_Process _FailAttack = null)
    {
        uInfo = _uInfo;
        attackTagName = _attackTagName;
        SuccessAttack = _SuccessAttack;
        FailAttack = _FailAttack;
    }
 
 
    public override void Updating()
    {
 
 
        if (!uInfo.unitData.isDie)
        {
            //레이쏘기
            ShotRay(uInfo.Get_Transform().position + addUpStartShootPosition, uInfo.Get_Transform().forward,
                uInfo.unitData.AttDistance, attackTagName, SuccessAttack, FailAttack);
        }
    }
 
 
 
    Color hitColor = Color.white;
    void ShotRay(Vector3 start, Vector3 direction, float distance, string tagName, Next_AttackProcess successNxt, Next_Process failNxt)
    {
 
        Debug.DrawRay(start, direction * distance, hitColor);
        hitColor = Color.white;
 
        bool isFind = false;
        RaycastHit[] hits = Physics.RaycastAll(start, direction, distance);
        
        for (int i = 0; i < hits.Length; i++)
        {
            if (isFind) break;
            
            if (string.Equals(hits[i].collider.tag, tagName))
            {
                isFind = true;
 
                        //공격성공시 콜백
                        if (successNxt != null)
                            successNxt(hits[i]);
 
 
                hitColor = Color.red;
            }
        
        }
 
        if (!isFind)
        {
            //공격실패시 콜백
            if (failNxt != null)
                failNxt();
        }
    }
 
}
cs


반응형
반응형

FPS 에서 총을쏘고나서 벽면에 파편효과 이펙트발생하려고 햇습니다. 

Raycast를 활용해 hit 된 벽면에 hitraycast 정보를 얻엇습니다. 그리고 그벽면에 파편이펙트 생성시켯습니다.


문제는 이펙트가 벽면 안쪽에서 발생되어 이펙트가 보이지 않게 되는문제가 있엇습니다.


이문제 해결 을위해서 이펙트를 hit된 벽면의 normal vector를 얻고 그 vector 만큼 회전 하도록 하엿습니다.


적절하게 회전할수 잇는방법 찾다가 Quaternion.FromToRotation 함수를 이용하엿습니다.



하지만 Quaternion.FromToRotation를 처음에 잘이해가 안되더군요 



* 아래 예시입니다


 

1
this.transform.rotation = Quaternion.FromToRotation(vector.up , vector.right );
cs



한오브젝트를 위처럼 회전한다고 하면 어떻게 회전하게 될까요 ??


매개변수의 의미를 하나하나 파악 해보자면 

Quaternion.FromToRotation(fromVector , toVector);


from 벡터 축에서 tovector 축으로 변형 하여 회전 한다 입니다. 의미는 즉,


아래 그림처럼

(0,1,0) 축을 (1,0,0)축으로 회전시킨다라는 말이 되는거지요 . (0,1,0) 축을 (1,0,0)축으로 회전시키게되면 z축 회전 90도 만큼 하게 되겟지요.


그러게 되면 해당 오브젝트 인스펙터 rotation 보면 z값이 90 으로 되어 있는것을 확인할수 있습니다.





아래 그림은 실제  이펙트발생시 회전시켜 적용시켯습니다.







Quaternion.FromToRotation을 적용 안한 이펙트의 로컬 좌표계 표시 입니다.

파랑색 z 방향을 잘보세요. 오른쪽 바깥쪾을 향해 있어 일부분가 이펙트가 벽면안에서 발생되고 잇네요






Quaternion.FromToRotation 적용한 로컬좌표계 표시입니다.

파랑색 z축이 벽면을향해 있어 이펙트가 벽면에서 전부다 보일수 있게 되엇네요 





아래는 실제 활용햇던 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
void SuccessShotAttack(RaycastHit hitInfo)
{
 
        if (hitInfo.transform != null)
        {
            //블릿마크
 
            GameObject mark = GameObject.Instantiate(Resources.Load("Prefabs/Effect/Effect_Bulletmark")) as GameObject;
            mark.transform.position = hitInfo.point;
          
            mark.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hitInfo.normal);
        }
}
cs


반응형
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System;
using System.Net;
 
 
public class SocketClient 
{
    public delegate void ReceiveDataPocess(byte[] packetData);
    public delegate void ErrorDataPocess();
    ReceiveDataPocess receiveProcess;
    ErrorDataPocess errorProcess;
 
    private Socket m_Socket;
 
    //리시브
    private byte[] BeginReceiveData;
 
 
 
 
 
 
    public SocketClient(ReceiveDataPocess _revProcess, ErrorDataPocess _errorProcess)
    {
        receiveProcess = _revProcess;
        errorProcess = _errorProcess;
 
    }
 
 
    #region Conneting
 
    public void StartConnect()
    {
 
        try
        {
            m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
 
            string hostIp = "192.111.1.1";
            int port = 11111;
 
            IPAddress curAdd = null;
 
            if (IPAddress.TryParse(hostIp, out curAdd))
            {
                Debug.Log("유효한 IP 입니다");
 
                m_Socket.BeginConnect(hostIp, port, Callback_ConnectEnd, null);
 
            }
 
 
        }
        catch(System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
    void Callback_ConnectEnd(IAsyncResult result)
    {
        try
        {
            m_Socket.EndConnect(result);
 
            //리시브 시작
            Begin_Recieve();
            Debug.Log("Socket Connect 성공 : " + m_Socket.Connected);
 
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
#endregion
 
 
 
 
 
 
 
 
 
 
    #region Recieving
    int BeginReceiveDataLength = 1024;
    void Begin_Recieve()
    {
        try
        {
            if(m_Socket != null)
            {
                BeginReceiveData = new byte[BeginReceiveDataLength]; //서버로 부터 받을 데이터 크기
                m_Socket.BeginReceive(BeginReceiveData, 0, BeginReceiveData.Length, SocketFlags.None, Callback_Received, BeginReceiveData);
            }
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
 
    void Callback_Received(IAsyncResult result)
    {
        if (m_Socket == null || !m_Socket.Connected) return;
 
 
        try
        {
 
            SocketError socketErr;
            int endReceiveLength = m_Socket.EndReceive(result,out socketErr);
 
            if(endReceiveLength <= 0)
            {
                
                //다시 beginReceive
                Begin_Recieve();
 
                return;
            }
 
            if(socketErr == SocketError.Success)
            {
                byte[] receivedBuffer = (byte[])result.AsyncState;
 
                //리시브 데이터 처리
                ProcessRecievedData(endReceiveLength, receivedBuffer);
 
                //다시 beginReceive
                Begin_Recieve();
            }
            else
            {
                Debug.LogError("Recieve Error : " + socketErr.ToString());
                return;
            }
 
 
 
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
    //리시브 데이터 처리
    void ProcessRecievedData(int _endReceivedBufferLength, byte[] _receivedBuffer)
    {
        try
        {
 
            Debug.LogError("Recieved Success , endReceiveLength : " + _endReceivedBufferLength);
 
            
            //실질 데이터 처리
            receiveProcess(_receivedBuffer);
 
            
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
    
 
#endregion
 
 
 
 
 
    #region Sending
    
    public void Send_ProtocolData( byte[] sendBodyData)
    {
        try
        {
            if(m_Socket.Connected && m_Socket != null)
            {
                
 
                //비동기로 데이터 보내기
                m_Socket.BeginSend(sendBodyData, 0, sendBodyData.Length, SocketFlags.None, Callback_SendProtocolData, null);
            }
 
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
 
 
    void Callback_SendProtocolData(IAsyncResult result)
    {
        try
        {
            m_Socket.EndSend(result);
        }
        catch (System.Exception ex)
        {
            ErrorException(ex);
        }
    }
 
    #endregion
    
 
 
    #region Error Exeption
 
    // ============= Error Exeption ===============
 
    void ErrorException(Exception ex)
    {
        Debug.LogError("Error_in Socket Processing : " + ex.ToString());
 
        errorProcess();
 
        DisConnect();
    }
 
    #endregion
 
 
    #region Disconnect
 
 
    public void DisConnect()
    {
        if (m_Socket == nullreturn;
 
        if(m_Socket.Connected) m_Socket.Shutdown(SocketShutdown.Both); // 소켓을 비활성화 시킵니다.
        m_Socket.Close(); //소켓을 닫습니다.
 
        Debug.LogError("Close networking");
    }
 
#endregion
 
 
}
 
cs


반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] Lock문  (0) 2018.12.18
[Socket] 소켓 메서드관련 설명  (0) 2018.12.14
[Socket] 소켓이란?  (0) 2018.12.14
반응형

비동기 Socket 사용시 (BeginConnect, EndConnect, BegineSend, EndSend, BeginReceive, EndReceive 매서드 사용시)


socket은 thread를 사용하기에 Receive로 받은 데이터(byte[]) 을 접근할때 유의해야한다.


간혹 동시에 mainThread (응용계층)에서 Receive데이터를 접근시 비동기 socket thread 에서 도 Receive데이터를 접근할수 있다.


위문제가 발생하면 Receive데이터 값을 처리하는데에 문제가 생길수 있다.


아래 lock문 예시로 동기화 처리를 하도록 하자.




lock정의 
- lock은 thread-unsafe(안전하지 않은 스레드)코드를 안전하게 사용하고자 할때 사용한다. 즉, 동기화 처리를 할때 사용하는 구문이다.


lock 사용법은 아래와 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Program
    {
        static object x = new object();
        static void AnotherMethod()
        {
            lock (x) { Console.WriteLine("Another method"); }
        }
        static void Main()
        {
            lock (x)
            {
                AnotherMethod();
               
            }
            Console.Read();
        }
    }
}
cs







필자는 비동기 socket 사용시 lock사용할떄 아래와같이 receive데이터를 접근하엿다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class SocketAsyncPool 
{
    private Queue<ByteData> m_DataPool;
 
 
    public SocketAsyncPool()
    {
        m_DataPool = new Queue<ByteData>();
    }
 

//들어온 receive데이터를 queue에 담는다.
    public void AddData(ByteData _addData)
    {
//이떄 lock 잠궈서 Enqueue한다.
        lock (m_DataPool)
            m_DataPool.Enqueue(_addData);
    }
 

// mainThread에서 저장된 receive데이터를 사용할떄
    public  ByteData GetData()
    {
//이떄 lock 잠궈서 Dequeue한다.
        lock (m_DataPool)
            return m_DataPool.Dequeue();
    }
 
    public bool ChkDataPool()
    {
        lock (m_DataPool)
            return m_DataPool.Count > 0 ? true : false;
    }
    
}
 

cs



주석 친 설명과같이  

lock (m_DataPool)


Lock 문 안에 처리를 하게되면 socket thread가 m_DataPool를 건들고 있을떄 mainThread는 m_DataPool 건들지 못하고 기다리게 된다.








반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] 소켓클라이언트 구현  (0) 2018.12.18
[Socket] 소켓 메서드관련 설명  (0) 2018.12.14
[Socket] 소켓이란?  (0) 2018.12.14
반응형


    * Socket.Connect()

 - Connect는 동기식이므로 연결이 이루어질 때까지 현재 스레드를 차단합니다.


    * Socket.BeginConnect()

 - BeginConnect는 비동기식입니다. 즉, 다른 스레드에서 연결을 수행하고 연결이 완료되면 이를 알립니다. 현재 호출 스레드를 차단하지 않습니다

   Connect 완료를 받으면 Socket.EndConnect을 호출해야합니다.


*Socket.EndConnect(result);

 - BeginConnect가 호출되고 성공적으로 콜백받게 되면 endConnect호출해야한다. 호출해주지 않으면 연결실패로 인한 재접속으로 불필요한     Connection을 발생하게 됩니다.


* Socket.BeginSend()

 비동기로 데이터 전송


*Socket.endSend()

 비동기 데이터 전송 완료시 호출해야한다.


* Socket.BeginReceive()

 비동기로 데이터 받는다. 성공적으로 콜백이 호출되면 endReceive를 바로 호출해야하고 이후 데이터처리 하면된다.


*socket.endReceive()

비동기 데이터 받기 완료시 호출해야한다.




반응형

'유니티 > 소켓 프로그래밍' 카테고리의 다른 글

[Socket] 소켓클라이언트 구현  (0) 2018.12.18
[Socket] Lock문  (0) 2018.12.18
[Socket] 소켓이란?  (0) 2018.12.14

+ Recent posts