어떤 종류의 게임 에셋이던지 보통 런타임 시점에 임시적으로 수정하기를 원할 것입니다. 예를 들어 캐릭터가 무적의 파워업을 획득했다면 무적 상태를 표현하기 위해 캐릭터 머티리얼 의 셰이더 를 변경하고 싶을 것입니다. 이 작업은 사용 중인 머티리얼의 수정을 수반합니다. 재생 모드 를 종료할 때 머티리얼이 다른 셰이더를 가지는 것을 원치 않으므로 이런 수정은 영구적이지 않습니다.
하지만 Unity에서 소스 에셋을 영구적으로 수정하는 스크립트를 작성할 수도 있습니다. 앞서 언급한 머티리얼을 출발점으로 활용해보겠습니다.
머티리얼의 셰이더를 일시적으로 변경하기 위해 머티리얼 컴포넌트의 셰이더 프로퍼티를 변경합니다.
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.material.shader = invincibleShader;
}
스크립트를 사용하고 플레이 모드를 종료하면 material 의 상태는 플레이 모드에 들어가기 이전으로 재설정됩니다. 이는 renderer.material에 액세스할 때마다 머티리얼이 자동으로 인스턴스화되고 인스턴스가 반환되기 때문입니다. 인스턴스는 동시에, 그리고 자동으로 렌더러에 적용됩니다. 따라서 영구적으로 반영되는 것을 걱정하지 않고 변경을 시도할 수 있습니다.
아래는 Unity에서 실제 소스 에셋 파일을 수정하는 메서드입니다. 하지만 이 작업은 수정 후 되돌릴 수 없으므로 신중하게 수행합니다.
플레이 모드를 종료할 때 머티리얼을 재설정하기 원하지 않는 경우에 대해 살펴보겠습니다. 이렇게 하려면 renderer.sharedMaterial를 활용합니다. sharedMaterial 프로퍼티는 해당(또는 그 외) 렌더러가 사용하는 실제 에셋을 반환합니다.
다음 코드는 머티리얼이 스페큘러 셰이더를 사용하도록 영구적으로 변경합니다. 머티리얼 상태를 플레이 모드 이전으로 재설정하지 않습니다.
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.sharedMaterial.shader = invincibleShader;
}
보는 바와 같이, sharedMaterial을 변경하는 것은 유용하면서도 리스크가 있습니다. sharedMaterial의 변경은 영구적이며 되돌릴 수 없습니다.
위에 설명한 바와 같은 방법을 머티리얼 외에도 적용할 수 있습니다. 해당 규칙을 따르는 에셋의 전체 리스트는 다음과 같습니다.
위 (Material, Mesh, Physic Material)클래스에 public 변수를 선언하고 관련 클래스 멤버 대신 해당 변수를 사용해 에셋을 수정하면 수정 사항이 적용되기 전에 자동 인스턴스화의 혜택을 누릴 수 없습니다.
수정할 때 절대 자동으로 인스턴스화가 되지 않는 두 개의 에셋이 있습니다.
스크립팅으로 해당 에셋에 수정하게 되면 영구적으로 남아 절대 되돌릴 수 없습니다. 따라서 스크립팅으로 터레인의 하이트맵을 변경하면 직접 인스턴스화를 확인하고 값을 할당해야 합니다. 텍스처도 마찬가지로, 텍스처 파일의 픽셀을 변경하면 해당 변경사항은 영구적으로 적용됩니다.
Texture2D 에셋은 iOS와 Android 프로젝트에서 수정할 때 자동으로 인스턴스화되지 않습니다. 스크립팅으로 해당 에셋을 수정한 사항은 영구적이며 취소되지 않습니다. 그러므로 텍스처 파일의 픽셀을 변경하면 해당 변경사항은 영구적으로 적용됩니다.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.