포슀트

πŸŒ” Unity Transform Position

http://smilejsu.tistory.com/560

https://velog.io/@csm2652/C-Struct%EC%97%90%EC%84%9C%EC%9D%98-NEW-%ED%82%A4%EC%9B%8C%EB%93%9C

https://answers.unity.com/questions/225729/gameobject-positionset-not-working.html

https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338/

https://answers.unity.com/questions/1033383/code-performance-when-to-use-new-on-vector3.html

https://stackoverflow.com/questions/18732930/how-is-vector3-implemented-why-are-the-properties-readonly

λ‹€λ£¨λŠ” 주제


  • transform.position = new Vector3(n, n, n);
    • 반볡적인 β€˜new’ Vector3 λŠ” νΌν¬λ¨ΌμŠ€μ— 영ν–₯을 주지 μ•ŠλŠ”κ°€?
    • μ™œ transform.position.Set() 은 μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ”κ°€?
    • μ™œ transform.position = new Vector3(n, n, n); 은 μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜λŠ”κ°€?
    • transform.position λŠ” μ–΄λ–»κ²Œ λŒμ•„κ°€λŠ” 걸까? (λ‡Œν”Όμ…œ)
    • transform.position.x λŠ” μ™œ readonly 인가?

반볡적인 β€˜new’ Vector3 λŠ” 퍼λͺ¨λ¨ΌμŠ€μ— 영ν–₯을 주지 μ•ŠλŠ”κ°€?


Vector3λŠ” Classκ°€ μ•„λ‹ˆλΌ Struct.
C#μ—μ„œ Struct 생성 μ‹œ new λ₯Ό μ‚¬μš©ν•˜λŠ” 것은, λ‹¨μˆœνžˆ ꡬ쑰체λ₯Ό μ΄ˆκΈ°ν™”ν•˜λŠ” 방법 쀑 ν•˜λ‚˜

μŠ€νƒμ— λ‹¨μˆœνžˆ Value κ°’ 생성 νŒŒκ΄΄ν•˜λŠ” 것이기 λ•Œλ¬Έμ—,
νž™μ— 클래슀 생성 νŒŒκ΄΄ν•  λ•Œμ²˜λŸΌ 가비지가 생기지 μ•ŠμŒ

μ°Έκ³  : λ¬Όλ‘  Struct μƒμ„±μžλŠ” 계속 호좜되고 있음 !

μ™œ transform.position.Set() 은 μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ”κ°€?

μ™œ transform.position = new Vector3(n, n, n); 은 μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜λŠ”κ°€?

transform.position λŠ” μ–΄λ–»κ²Œ λŒμ•„κ°€λŠ” 걸까? (μœ μΆ”)


transform.position.Set() 은 μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ”λ‹€.
근데 또 transform.position = new Vector3(n, n, n); λŠ” μ˜λ„λŒ€λ‘œ μž‘λ™ν•œλ‹€.
이건 처음 μœ λ‹ˆν‹°λ₯Ό λ‹€λ£° λ•Œ κ²ͺκ²Œλ˜λŠ” κ²½ν—˜κ³Ό 사싀

transform.position은 ν”„λ‘œνΌν‹°μΈκ²Œ μ•„λ‹κΉŒ?

1
2
3
4
5
6
7
8
private Vector3 realPosition;
public Vector3 position
{
	get => realPosition;
	set => realPosition = value;
}

Vector3λŠ” μœ„μ—μ„œ μ–ΈκΈ‰ν–ˆλ“― Struct

transform.position μ—­μ‹œ Vector3, Struct μ΄λ―€λ‘œ,
transform.position으둜 Getν•œ Vector3λŠ”, transform λ‚΄λΆ€μ—μ„œ μž‘λ™ν•˜λŠ” μ‹€μ œ position이 μ•„λ‹ˆλΌ,
μ‹€μ œ positionκ³Ό 값이 λ˜‘κ°™μ€, μ‹€μ œ position의 볡사본 Vector3이 λ¦¬ν„΄λ˜λŠ” 것

λ•Œλ¬Έμ— λ³΅μ œλ˜μ–΄ return된 Vector3λ₯Ό 아무리 Set() 해도, μ‹€μ œ transform.position에 영ν–₯을 주지 μ•ŠμŒ.

λ°˜λŒ€λ‘œ λŒ€μž…μ—°μ‚°μžλ‘œ ν”„λ‘œνΌν‹°μ— μ§μ ‘μ μœΌλ‘œ Set ν•  λ•ŒλŠ”,
λ„˜κ²¨μ€€ Vector3κ°€ ν”„λ‘œνΌν‹°κ°€ κ°€λ¦¬ν‚€λŠ” μ‹€μ œ ν¬μ§€μ…˜ 값에 λŒ€μž…ν•˜κΈ° λ•Œλ¬Έμ— μ˜λ„λŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•Šμ„κΉŒ μ‹ΆμŒ !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private Vector3 realPosition;
public Vector3 position
{
	get
	{
		return μ›”λ“œ μ’Œν‘œκ³„ κΈ°μ€€ position;
	}
	set
	{
		realPosition = 둜컬 μ’Œν‘œκ³„ κΈ°μ€€ value;
		였브젝트 μœ„μΉ˜ 처리();
	}
}

λ¬Όλ‘  !

μ‹€μ œλ‘œ λ‚΄λΆ€ μ²˜λ¦¬κ°€ 어떀지 잘 λͺ¨λ₯΄κ² μ§€λ§Œ,
μœ„μ²˜λŸΌ 같은 일련의 μ²˜λ¦¬κ³Όμ •μ΄ 더 λ³΅μž‘ν•˜κ²Œ μžˆμ§€ μ•Šμ„κΉŒ μ‹Άλ‹€

transform.position.x λŠ” μ™œ readonly 인가?


transform.position은 Vector3, Struct

transform.position으둜 Getν•˜μ—¬ κ°€μ Έμ˜¨ 볡제된 Vector3λŠ”,
원본이 μ—†λŠ”, λ³€μˆ˜κ°€ μ•„λ‹Œ, μ‚¬λŠ” 곡간이 μ—†λŠ” Struct β€˜κ°’β€™

Struct와 같은 κ°’ ν˜•μ‹μΈ int에 λŒ€ν•˜μ—¬, 100 = 0; 이 의미 μ—†λŠ” λͺ…령인 것 처럼,
Struct도 β€˜κ°’β€™ 에 값을 λŒ€μž…ν•˜λŠ” 일은 μ˜λ―Έκ°€ μ—†λ‹€

1
2
SomeStruct ss;
ss.SomeVar = 1;

이건 μ˜λ―Έκ°€ μžˆλ‹€
ss λΌλŠ” SomeStruct λ³€μˆ˜μ˜ SomeVar 값을 λ°”κΎΈκ³  μ €μž₯ν•œλ‹€.
κ²°κ³Όκ°€ λ‚¨λŠ”λ‹€.

이 κΈ°μ‚¬λŠ” μ €μž‘κΆŒμžμ˜ CC BY 4.0 λΌμ΄μ„ΌμŠ€λ₯Ό λ”°λ¦…λ‹ˆλ‹€.