2

ゲーム オブジェクトに一種の「マスター スクリプト」として単純なタッチ/マウスクリック スクリプトをアタッチしました。つまり、ゲーム オブジェクトは非表示で、ゲームの実行中はこのタッチ スクリプトを保持するだけで何もしません。

実行時に生成される他の名前付きゲームオブジェクトに、このマスター スクリプトからタッチ/クリックしたときにハイライトするなどのことを指示するにはどうすればよいですか?

ハイライトのスクリプトは次のようです: renderer.material.color= colorRed;

しかし、クリックされたゲームオブジェクトがマスタースクリプトから強調表示されるようにする方法がわかりません。

助けてください!(C# でプログラミングしています) ありがとうございます。

4

3 に答える 3

1

わかりましたので、GUI で行っていない場合は、レイ キャストを使用することをお勧めします。Unity Ray Castingを確認してから使用してください

hit.transform.gameObject.renderer.material.color = red;

次のようなスイッチを使用できます。

if (hit.transform.gameObject.CompareTag("tag")) {
    // turn to red;
} else {
    // turn to white;
}

何をしているかに応じてScreenPointToRayまたはを使用します。ScreenPointToWorld

タッチの場合、次のようになります。

void Update () {
    foreach (Touch touch in Input.touches)
    {
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 1000.0f))
        {
            if (hit.collider.gameObject.CompareTag("tag"))
            {
                hit.collider.gameObject.renderer.material.color = red;
            }
        }
    }
    // You can also add in a "go back" feature in the update but this will "go back" then when the touch ends or moves off
    // Also not so good to search for Objects in the update function but that's at your discretion.
    GameObject[] gObjs = GameObject.FindGameObjectsWithTag("tag");
    foreach (GameObject go in gObjs) {
        go.renderer.material.color = white;
    }
}

「マネージャー」へのpingに関する質問に答えるには

私は2つのオプションのうちの1つを行います。また:

// Drop the object containing YourManager into the box in the inspector where it says "manage"

public YourManager manage;

// In the update and in the Ray Cast function (right next to your color change line):
manager.yourCall ();
// and
manager.yourString = "cool";

また

private YourManager manage;

void Awake () {
    manager = GameObject.FindObjectWithTag("manager").GetComponent<YourManager> ();
}

// In the update and in the Ray Cast function (right next to your color change line):

// In your manager file have "public bool selected;" at the top so you can access that bool from this file like:
manager.selected = true;

これについては、別の回答で少し詳しく説明しますHERE

マウス クリックの場合は、次のような MonoDevelop 関数をチェックします。

// This file would be on the game object in the scene
// When the mouse is hovering the GameObject
void OnMouseEnter () {
    selected = true;
    renderer.material.color = red;
}

// When the mouse moved out
void OnMouseExit () {
    selected = false;
    renderer.material.color = white;
}

// Or you can use the same system as above with the:
Input.GetMouseButtonDown(0))

解像度:

マネージャー ファイルで bool を使用するtrueが選択されfalseていません。インスタンス化するすべてのオブジェクトにタグを付け、マスター ファイルからゲーム オブジェクトへのレイ キャストを使用します。そのタグが付いたゲーム オブジェクトになったら、色を交換し、マスター ファイルから bool を取得します。おそらくマスターファイルから内部的に行う方が良いでしょう。(すべてはあなたが何をしているかに依存します)

于 2015-05-20T10:48:32.950 に答える
0

これを行う最も明白な方法は、プレハブとレイヤーまたはタグを使用することです。プレハブにタグを追加する (「選択可能」など) か、プレハブを「選択可能」レイヤーに移動してから、このレイヤーにコードを記述して、選択可能なすべてのアイテムがこのレイヤー上にある/このタグがあることを認識します。

これを行う別の方法 (私の意見では、これもより良い方法です) は、カスタムの「選択可能」コンポーネントを実装することです。クリックしたアイテムでこのコンポーネントを検索し、そのコンポーネントが見つかった場合は選択を実行します。この方法は、選択マスター スクリプトに常駐するこのコンポーネントにいくつかの選択ロジックを追加できるため、より優れています (いくつかの選択可能要素を追加した後のスクリプトのサイズをイメージしてください)。

SelectableItem スクリプト (名前は任意) とその派生物をいくつか実装することで、これを行うことができます。

public class SelectableItem : MonoBehavior {

    public virtual void OnSelected() {
        renderer.material.color = red;  
    }
}

public class SpecificSelectable : SelectableItem {

    public override void OnSelected() {
        //You can do whatever you want in here
        renderer.material.color = green;
    }
}

//Adding new selectables is easy and doesn't require adding more code to your master script.
public class AnotherSpecificSelectable : SelectableItem {

    public override void OnSelected() {
        renderer.material.color = yellow;
    }
}

そしてあなたのマスタースクリプトで:

// Somewhere in your master selection script
// These values are arbitrary and this whole mask thing is optional, but would improve your performance when you click around a lot.
var selectablesLayer = 8;
var selectablesMask = 1 << selectablesLayer;

//Use layers and layer masks to only raycast agains selectable objects.
//And avoid unnecessary GetComponent() calls.
if (Physics.Raycast(ray, out hit, 1000.0f, selectablesMask))
{
    var selectable = hit.collider.gameObject.GetComponent<SelectableItem>();
    if (selectable) {
        // This one would turn item red or green or yellow depending on which type of SelectableItem this is (which is controlled by which component this GameObject has)
        // This is called polymorphic dispatch and is one of the reasons we love Object Oriented design so much.
        selectable.OnSelected();
    }
}

したがって、いくつかの異なる選択可能項目があり、選択時に異なることを実行したいとします。これはあなたがこれを行う方法です。選択可能なものの中には、選択コンポーネントの 1 つを持つものもあれば、別のものを持つものもあります。選択を実行するロジックはマスター スクリプトにありますが、実行する必要があるアクションは、ゲーム オブジェクトに関連付けられた特定のスクリプトにあります。

さらに進んで、これらの Selectables に OnUnselect() アクションを追加できます。

public class SelectableItem : MonoBehaviour {
    public virtual void OnSelected() {
        renderer.material.color = red;
    }

    public virtual void OnUnselected() {
        renderer.material.color = white;
    }
}

そして、次のようなこともします:

//In your master script:
private SelectableItem currentSelection;

var selectable = hit.collider.gameObject.GetComponent<SelectableItem>();
if (selectable) {
    if (currentSelection) currentSelection.OnUnselected();
    selectable.OnSelected();
    CurrentSelection = selectable;
}

そして、選択解除ロジックを追加しました。

免責事項: これらは単なるスニペットの集まりです。それらをコピーして貼り付けるだけでは、おそらくすぐには機能しません。

于 2016-02-08T08:44:28.647 に答える