码迷,mamicode.com
首页 > 其他好文 > 详细

根据XX对XX产生行为的方法

时间:2017-08-30 22:30:03      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:执行   unity   cti   委托   方法   bsp   engines   raw   name   

可以用委托来搞定 

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public delegate void u3d(string name);
   public class DM02Brridge:MonoBehaviour
    {
    private void Start()
    {
        u3d AA = new DirectX().Render ;
        Sphere l_Sphere = new Sphere();
        l_Sphere.Draw(new DirectX().Render);这样的话只需要添加新执行方法的话,new一下即可
        l_Sphere.Draw(new OpenGL().Render);
        Cube l_Cube = new Cube();
        l_Cube.Draw();
    }

}

public class Sphere //球体的类,有自己的属性
{
    public string name = "Sphere";
    public OpenGL openGL = new OpenGL();

    public void Draw(u3d AA)
    {
        AA(name);
    }
}
public class OpenGL//绘制的引擎
{
    public void Render(string name)
    {
        Debug.Log("OpenGL绘制了:" + name);
    }

}
public class Cube //球体的类,有自己的属性
{
    public string name = "Cube";
    public OpenGL openGL = new OpenGL();

    public void Draw()
    {
        openGL.Render(name);
    }
}
public class DirectX
{
    public void Render(string name)
    {
        Debug.Log("DrectX绘制了:" + name);
    }
}

也可以使用父类为接口继承的方法

 

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public delegate void u3d(string name);
   public class DM02Brridge:MonoBehaviour
    {
    private void Start()
    {
        EngineSystem AA = new OpenGL();
        Cube BB = new Cube(AA);
        BB.Draw();
        AA = new DirectX();
        Sphere SP = new Sphere(AA);
        SP.Draw();
    }

}
public class IHshaPe
{
    public string name;
    public EngineSystem Eng;
    public IHshaPe(EngineSystem l_eng)
    {
        Eng = l_eng;
    }
    public void Draw()
    {
        Eng.DrawEng(name);
    }
}
public abstract class EngineSystem
{
    public abstract void DrawEng(string name);
}

public class Sphere : IHshaPe
{

    public Sphere(EngineSystem l_eng) : base(l_eng)
    {
        base.name = "Sphere";
    }
}
public class OpenGL:EngineSystem
{
    public override void DrawEng(string name)
    {
        Debug.Log("OpenGL绘制了:" + name);
    }

}
public class Cube:IHshaPe //球体的类,有自己的属性
{


    public Cube(EngineSystem l_eng) : base(l_eng)
    {
        name = "Cube";
    }

}
public class DirectX:EngineSystem
{
    public override void DrawEng(string name)
    {
        Debug.Log("DrectX绘制了:" + name);
    }
}

 

根据XX对XX产生行为的方法

标签:执行   unity   cti   委托   方法   bsp   engines   raw   name   

原文地址:http://www.cnblogs.com/signzun/p/7455511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!