如何在RUNTIME GameObject UNITY下制作RUNTIME BUTTON

时间:2012-10-18 06:25:31

标签: unity3d

有没有办法在运行时游戏对象下创建运行时按钮?我能够用UIToolkit中的所有组件/材料继承运行时游戏对象。

继承我的代码:

    public class loadImage : MonoBehaviour
{

    public UIToolkit duplicate_kit;
    public Material material;
    public UIButton b1, b2;
    public MeshFilter meshFilter;
    public MeshRenderer meshRenderer;
    public Mesh _mesh;


    void initKit()
    {

        Material duplicateMaterial = new Material (material.shader);
        duplicateMaterial.CopyPropertiesFromMaterial(material);
        material = duplicateMaterial;

        GameObject loadImageToolkit = new GameObject("duplicate_toolkit");
        loadImageToolkit.transform.parent = GameObject.Find("UI").transform;
        loadImageToolkit.layer = LayerMask.NameToLayer("UILayer");


        duplicate_kit = loadImageToolkit.AddComponent<UIToolkit>() ;
        duplicate_kit.texturePackerConfigName = "friendsImageSheet";
        duplicate_kit.material = duplicateMaterial;

        meshRenderer = loadImageToolkit.AddComponent<MeshRenderer>();
        meshRenderer.renderer.material = duplicateMaterial;

        meshFilter = loadImageToolkit.AddComponent<MeshFilter>();
        _mesh = meshFilter.mesh;


    }

    void Start()
    {
         initKit();

//       b1 = UIButton.create( duplicate_kit, "FriendsAvatar.png","FriendsAvatar.png",0,0); 
//       b1.pixelsFromTopLeft ( 60, 0 );
//       StartCoroutine(getImage(b1));


         b2 = UIButton.create( duplicate_kit, "FriendsAvatar.png", "FriendsAvatar.png" , 0,0 );
         b2.pixelsFromTopLeft( 120, 0 );
         StartCoroutine(getImage(b2));


         b1 = UIButton.create( (UIToolkit)GameObject.Find("duplicate_toolkit").GetComponent(typeof(UIToolkit)), "FriendsAvatar.png","FriendsAvatar.png",0,0);   
         b1.pixelsFromTopLeft ( 120, 0 );
         StartCoroutine(getImage(b1));

    }

    IEnumerator getImage(UIButton uib)
    {      
        WWW imageRequest = new WWW("https://graph.facebook.com/xxxxxxxxx/picture?type=small"); 
        yield return imageRequest;
        Texture tex = imageRequest.texture;

        uib.manager.renderer.material.mainTextureScale = new Vector2(18.0f,20.05f);
        uib.manager.renderer.material.mainTexture = tex;
    }

}

主要目标是在dup_toolkit(游戏对象)下出现b2按钮

就像:

duplicate_toolkit

  

UIButton

enter image description here

1 个答案:

答案 0 :(得分:0)

我没有使用过UITookit,但总的来说,你通过连接Transform组件在Unity中做到这一点:

b2.transform.parent = duplicate_toolkit.transform;