Unity脚本:Renderer Sprites组件

时间:2014-01-21 16:58:07

标签: unity3d unityscript

我正在使用Brackeys here的库存系统,然后我尝试自定义设备对象,就像文档说的那样。当对象拖动到武器架(库存)时,我需要该项启用渲染器精灵组件,并在对象从武器架拖出时禁用渲染器精灵。我是实现GetComponent(SpriteRenderer).enabled = true;用于激活渲染器Sprites和GetComponent(SpriteRenderer).enabled = false;禁用,但脚本似乎不起作用,当对象从库存中拖出时,Renderer Sprites Component仍然处于活动状态。这里是启用/禁用Renderer Sprites

的脚本部分
#pragma strict

//This script allows you to create equipment effects that will be called either OnEquip or WhileEquipped. This is usefull for magic effects and stat handling.

@script AddComponentMenu ("Inventory/Items/Equipment Effect")
@script RequireComponent(Item)

private var effectActive = false;

function Update () 
{
    if (effectActive == true)
    {
       //-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE AS LONG AS THE ITEM IS EQUIPPED. <-----


    }
}

function EquipmentEffectToggle (effectIs : boolean)
{
    if (effectIs == true)
    {
       effectActive = true;

       Debug.LogWarning("Remember to insert code for the EquipmentEffect script you have attached to " + transform.name + ".");

       //-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE JUST WHEN THE ITEM IS EQUIPPED. <-----

         GetComponent(SpriteRenderer).enabled = true;




    }
    else
    {
       effectActive = false;

       //-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE JUST WHEN THE ITEM IS UNEQUIPPED. <-----

         GetComponent(SpriteRenderer).enabled = false;

       //Destroy(gameObject);

    }
}

是正确的实施吗?我该怎么办?

1 个答案:

答案 0 :(得分:0)

你应该阅读碰撞和触发器....让它们在脚本中启用或禁用渲染器。或者更好地让清单做到这一点....我不认为它们对你的方式非常有效正在努力。

即。将库存设置为触发器并使OnTriggerEnter(对撞机col)执行col.gameObject.GetComponent()。enabled = true;

和OnTriggerExit(对撞机col)执行col.gameObject.GetComponent()。enabled = false;

如果您使用Unity的2D组件,那么它是OnTriggerEnter2D(Collider2D col)等等..... 这只是一个例子,远非100%完全代码。只是推动一种可能的方式。你可能需要设置更多东西。