2016-10-09 11 views
1

私のユニティベースのAndroidゲームでは、各レベルの質問の数に基づいて画像を動的に追加したいと考えています。参照のために画像が示されています。それぞれの正解は緑色で、間違ったものは赤色で表示されます。私は統一に慣れていないし、これを達成するための措置を見つけようと努力している。実行時に画像を動的に追加 - Unity 5

この要件の例に関するヘルプは大きな助けになります。

enter image description here

答えて

1

私はかつて、動的に各レベルに基づいて、ボタンを作成するためのスクリプトを書きました。私がしたことは、シーンの最初のボタンを作成し、最初のボタンに基づいて他のボタンを追加することでした。以下は、私のコードのシェルです:

// tutorialButton and levelButtons are public variables which can be set from Inspector 
RectTransform rect = tutorialButton.GetComponent<RectTransform>(); 

for (int i = 1; i < levelSize; i++) { 
    // Instantiate the button dynamically 
    GameObject newButton = GameObject.Instantiate (tutorialButton); 

    // Set the parent of the new button (In my case, the parent of tutorialButton) 
    newButton.transform.SetParent (levelButtons.transform); 

    //Set the scale to be the same as the tutorialButton 
    newButton.transform.localScale = tutorialButton.transform.localScale; 

    //Set the position to the right of the tutorialButton 
    Vector3 position = tutorialButton.transform.localPosition; 
    position.x += rect.rect.width*i; 
    newButton.transform.localPosition = position; 
} 

私はそれは、異なる画面サイズやキャンバスに応じて、予期しない結果が得られない場合があり、うまくいけば、それはあなた与えるので、これは正しいアプローチであれば、正確にわかりません動的にオブジェクトを作成するアイデア

+0

このスクリプトを添付する場所。キャンバスやパネルで? – iappmaker

+0

inspectorからtutorialButtonオブジェクトとlevelButtonオブジェクトを設定している限り、実際にスクリプトをどこに添付するかは関係ありません。スクリプトが添付されているGameObjectのプロパティは使用されません。 (私の場合は、すべてのゲームロジックを扱うスクリプトがあり、それは空のGameObjectに付属していて、このコードはStart()関数内にあったので、各レベル(シーン)のロード時に一度呼び出され、レベルに必要なボタンを作成しました)。 – merterpam

1

私はこのことができますかどうかわからないんだけど、あなたはこれで、キャンバスの下にシーン内のすべての画像を持っている場合は、単にスクリプトでキャンバスをドラッグして、

//level-1 is to keep the array notation FindObjectOfType<NameOfScript>.ChangeColor(level-1,Color.green);

を使用する必要があります

//Keep it private but you still see it in inspector 
//#Encapsulation :) 
[SerializeField] 
private Canvas _canvas; 

private Image[] _images; 
//keep the original colors in case you want to change back 
private Color[] _origColors; 

void Start() { 
    _images = GetComponentsInChildren<Image>(); 
    _origColors = new Color[_images.Length]; 
    for (int i = 0; i < _images.Length; i++) 
    { 
     _origColors[i] = _images[i].color; 
    } 

} 
//Reverts the color of the image back to the original 
public void RevertToOriginal(int imageIndex) 
{ 
    _images[imageIndex].color = _origColors[imageIndex]; 
} 
//Change to color to the coresponding index, starts from 0 
public void ChangeColor(int imageIndex, Color color) 
{ 
    _images[imageIndex].color = color; 
} 
:またはあなたがこれはスクリプトである

//level-1 is to keep the array notation FindObjectOfType<NameOfScript>.RevertColor(level - 1);

も行うことができます

P.S最後にのみ表示する場合は、キャンバスに=(trueまたはfalse)を有効にするメソッドを作成できます。したがって、レベルの最後までfalseを保持し、表示するときはtrueにします。すべての応答の後に、結果に応じてChangeColorを呼び出します。

NameOfScript variableName = FindObjectOfType<NameOfScript>(); 

その後あなただけのスクリプトを置く場所も、それは問題ではありません

variableName.ChangeColor(level - 1, Color.green); 

を呼び出すを:それは簡単にあなたが使用できるようにするに 。シーンの中に何らかのマネージャー(空のGameObject)を作ってそこに置くことにしました。

関連する問題