2014-12-22 2 views
2

그래서 여러 곳에서 무작위로 반복적으로 사용하는 함수가 있습니다. 그리고 게터와 세터도 있습니다. 이 같은C# 코드 삽입?

예를 들어 단지 물건 : 당신이 말할 수없는 경우

public GameObject GameObjectCache 
{ 
    get 
    { 
     if (gameObjectCache == null) 
      gameObjectCache = this.gameObject; 
     return gameObjectCache; 
    } 
} 
private GameObject gameObjectCache; 

public Transform TransformCache 
{ 
    get 
    { 
     if (transformCache == null) 
      transformCache = this.GetComponent<Transform>(); 
     return transformCache; 
    } 
} 
private Transform transformCache; 

이 유니티입니다.

내가 실제로하고 싶은 것은 그 기능을 사용하여 다른 곳으로 옮기는 것입니다. 그럼 내 수업에 그냥

[TransformCache] 

일종의 한 줄 태그 및 다른 클래스에서 인라인 내 클래스에 배치 할 것입니다.

Mono.Cecil을 사용하여이 작업을 수행 할 수있는 복잡한 방법이 있다는 것을 알고 있습니다. 링크가 마음에 드는 간단한 튜토리얼이 있다면 누구나 알고 있습니다.

하지만 그보다 쉬운 방법이 있습니까? 나는 C와 Objective-C를 안다. 심지어 CG 코드도 이것을 수행하는 함수를 가지고있다. 어쨌든 C#에서 쉽게이 작업을 수행 할 수 있습니까?

+0

PostSharp를 살펴보십시오. http://www.postsharp.net/ –

+0

코드 삽입 또는 종속성 삽입이 필요합니까? 의존성 관리를위한 MS Unity 프레임 워크를 살펴보십시오. –

+0

MEF가 화합하여 작동하는지 확실하지 않지만 원하는 것을 할 수 있습니다. – JRLambert

답변

1

이 방법이 도움이되는지는 모르겠지만 원하는 일반적인 것들을 래퍼 클래스에 배치 한 다음 다른 게임 개체에 클래스를 추가하는 방법은 무엇입니까? 당신이

public class YourOtherClass : GameObject 
{ 
    MyWrapper mywrapper; 

    public Start() 
    { 
     // instantiate the wrapper object with the game object as the basis 
     myWrapper = new MyWrapper(this); 

     // then you can get the game and transform cache objects via 
     GameObject cache1 = myWrapper.GameObjectCache; 
     Transform tcache1 = myWrapper.TransformCache; 
    } 
} 

죄송합니다 ... C에서 + + 당신은 본질적으로 그런 일을 허용 할 수있는 여러 클래스에서 파생 할 수 사용하게 될 수업에

다음
public class MyWrapper 
{ 
    private GameObject parentGameObj; 
    public MyWrapper(GameObject srcObj) 
    { 
     parentGameObj = srcObj; 
    } 

    public GameObject GameObjectCache 
    { 
     get 
     { 
      if (gameObjectCache == null) 
       gameObjectCache = parentGameObj.gameObject; 
      return gameObjectCache; 
     } 
    } 
    private GameObject gameObjectCache; 

    public Transform TransformCache 
    { 
     get 
     { 
      if (transformCache == null) 
       transformCache = parentGameObj.GetComponent<Transform>(); 
      return transformCache; 
     } 
    } 
    private Transform transformCache; 
} 

같은 뭔가. 내가 생각할 수있는 유일한 다른 점은 GetComponent() 호출을 통해 반복되는 유사한 유형을 함수가 사용하는 경우 Generics를 사용하는 것이다.

+0

저는 이것을 많이 해왔습니다. 어느 정도 그것은 좋을 수 있습니다. 그러나 내가 오프닝 포스트에서 설명하는 방법은 클래스가 어떻게 끝나는지에 대한 코드를 명확하게 허용 할 것이다. – rygo6

+0

@techmage, 답안 하단의 개정 노트. – DRapp

+0

나는 이것을 사용하기 시작했으며 실제로 그것을 아주 좋아한다. 고맙습니다. – rygo6