通过类似GetComponent从组件中直接获得接口的三种方式
2016-10-28 09:41
495 查看
孙广东 2016.10.15 http://blog.csdn.net/u010019717
功能类似于 GetComponents 等函数:
1、 不用接口, 使用抽象类 继承自 Monobehaiour
public abstract class LayerPanelBase :MonoBehaviour
{
public abstract void InitView(HeroModel heroModel, CharacterAttributeView characterAttributeView);
}
然后执行 .GetComponent<LayerPanelBase>().InitView(myHeroModel, this);
2、原理类似 网上百度可见的:
原帖:
http://forum.unity3d.com/threads/60596-GetComponents-Possible-to-use-with-C-Interfaces
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class GameObjectEx
{
public static T GetInterface<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an actual interface!");
return null;
}
var tmps = inObj.GetComponents<Component>().OfType<T>();
if (tmps.Count()==0) return null;
return tmps.First();
}
public static IEnumerable<T> GetInterfaces<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an actual interface!");
return Enumerable.Empty<T>();
}
return inObj.GetComponents<Component>().OfType<T>();
}
}
定义的时候直接使用 Interface 这种方式最好了!
3、使用Linq :
var controllers = GetComponents<MonoBehaviour>()
.Where(item => item is IInfiniteScrollSetup)
.Select(item => item as IInfiniteScrollSetup)
.ToList();
扩展方法:
public static List<T> GetInterfaces<T>(this GameObject obj) where T: class
{
return obj.GetComponents<MonoBehaviour>()
.Where(item => item is T)
.Select(item => item as T)
.ToList();
}
没办法提供 GetComponent 这种 找一个的方式。
所以相对来来讲还是 第二种 GetInterface 找接口的一个
GetInterfaces 找接口的多个!
http://blog.csdn.net/u010019717
功能类似于 GetComponents 等函数:
1、 不用接口, 使用抽象类 继承自 Monobehaiour
public abstract class LayerPanelBase :MonoBehaviour
{
public abstract void InitView(HeroModel heroModel, CharacterAttributeView characterAttributeView);
}
然后执行 .GetComponent<LayerPanelBase>().InitView(myHeroModel, this);
2、原理类似 网上百度可见的:
原帖:
http://forum.unity3d.com/threads/60596-GetComponents-Possible-to-use-with-C-Interfaces
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class GameObjectEx
{
public static T GetInterface<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an actual interface!");
return null;
}
var tmps = inObj.GetComponents<Component>().OfType<T>();
if (tmps.Count()==0) return null;
return tmps.First();
}
public static IEnumerable<T> GetInterfaces<T>(this GameObject inObj) where T : class
{
if (!typeof(T).IsInterface)
{
Debug.LogError(typeof(T).ToString() + ": is not an actual interface!");
return Enumerable.Empty<T>();
}
return inObj.GetComponents<Component>().OfType<T>();
}
}
定义的时候直接使用 Interface 这种方式最好了!
3、使用Linq :
var controllers = GetComponents<MonoBehaviour>()
.Where(item => item is IInfiniteScrollSetup)
.Select(item => item as IInfiniteScrollSetup)
.ToList();
扩展方法:
public static List<T> GetInterfaces<T>(this GameObject obj) where T: class
{
return obj.GetComponents<MonoBehaviour>()
.Where(item => item is T)
.Select(item => item as T)
.ToList();
}
没办法提供 GetComponent 这种 找一个的方式。
所以相对来来讲还是 第二种 GetInterface 找接口的一个
GetInterfaces 找接口的多个!
http://blog.csdn.net/u010019717
相关文章推荐
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- 通过HttpClinet类用get方法获得远程接口返回的Json串
- jQuery.getJSON(url, [data], [callback])[通过url,文件。txt,带参数三种方式](十三)
- 02_3中方式的反射,通过Class.forName获得Class对象,通过类.class获得字节码对象,通过类实例.getClass()的方式获得Class对象
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- 创建Bean的三种方式 在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法。实际上,Sprin
- 02_3中方式的反射,通过Class.forName获得Class对象,通过类.class获得字节码对象,通过类实例.getClass()的方式获得Class对象
- (一)获取类的三种方式,根据类获取成员变量,成员方法,直接实现的接口,自己的构造方法,直接的父类
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- 直接将ip格式的地址通过 get 方式在写在url中 出现的错误。
- js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
- Java web项目中,通过get方式传中文参数时,获得的参数为乱码的解决方案
- JS通过Get方式调用接口编码问题
- 通过接口方式直接反射其接口类
- VC环境下DLL接口申明的三种方式 supconsupcon的专栏
- VC环境下DLL接口申明的三种方式
- .NET中的三种接口实现方式
- 通过三种方法为apt-get设置http代理服务
- 如何通过代码直接获得Spring容器中的Bean?
- Java调用Delphi DLL(通过C++调用和直接调用两种方式)