C# 将按钮添加到Unity视图
我试图在Eclipse中为我的Unity视图添加一个按钮。我已经导入了Unity项目,并且更改了onCreate方法,因此我的setContentView设置了自己的xml布局,而不是Unity视图。然后我将Unity视图添加到此布局中。这是可行的,但不是我的Unity视图顶部的按钮,而是在屏幕的一侧。请看下面我的代码。这是我的onCreate方法:C# 将按钮添加到Unity视图,c#,android,eclipse,unity3d,android-xml,C#,Android,Eclipse,Unity3d,Android Xml,我试图在Eclipse中为我的Unity视图添加一个按钮。我已经导入了Unity项目,并且更改了onCreate方法,因此我的setContentView设置了自己的xml布局,而不是Unity视图。然后我将Unity视图添加到此布局中。这是可行的,但不是我的Unity视图顶部的按钮,而是在屏幕的一侧。请看下面我的代码。这是我的onCreate方法: protected void onCreate(Bundle savedInstanceState) { mUnityPlayer
protected void onCreate(Bundle savedInstanceState) {
mUnityPlayer = new UnityPlayer(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
if (mUnityPlayer.getSettings().getBoolean("hide_status_bar", true))
getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
UnityPlayer.UnitySendMessage("Character_Mesh", "LoadScene", "CM_Unity_Elf");
View playerView = mUnityPlayer.getView();
detector = new GestureDetector(this, this);
detector.setOnDoubleTapListener(this);
setContentView(R.layout.menu_buttons);
LinearLayout lLayout = (LinearLayout) findViewById(R.id.rlContainer);
lLayout.addView(playerView);
playerView.setOnTouchListener(this);
playerView.requestFocus();
}
这是我的xml布局菜单按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_call" />
</LinearLayout>
有没有办法可以在Unity视图的顶部添加一个按钮,而不是在它的侧面添加一个按钮?我自己想出了这个方法。您可以膨胀自己的RelativeLayout并将其添加到Unity视图,然后将Unity视图设置为setContentView,而不是将Unity视图添加到您自己的布局中
RelativeLayout layout = (RelativeLayout) View.inflate(this, R.layout.menu_buttons, null);
mUnityPlayer.addView(layout);
View playerView = mUnityPlayer.getView();
setContentView(playerView);
要为您使用的Unity视图设置OnTouchListener,请执行以下操作:
playerView.setOnTouchListener(this);
感谢您发布此消息-我能够让您描述的工作正常,但是您是如何让菜单按钮接收触摸事件的?据我所知,Unity是一个本机应用程序,因此它在java代码中使用本机触控事件并忽略我的触控事件调用,谢谢!我曾经是一个听者。您只需确保为UnityView设置OnTouchlistener。我将编辑上面的代码并向您展示我是如何做到的。嗯,似乎没有做到这一点-(setOnTouchListener获取setOnTouchListener而不是此)RelativeLayout=(RelativeLayout)视图。充气(this,R.layout.layout,null);layout.setLayoutParams(新的LayoutParams(300100));布局。添加视图(b);mUnityPlayer.addView(布局);View playerView=mUnityPlayer.getView();setContentView(playerView);playerView.setOnTouchListener(新的OnTouchListener(){@Override public boolean onTouch(视图v,运动事件){Toast.makeText(getApplicationContext(),“HI”,Toast.LENGTH_LONG).show();return true;});我对此提出了一个堆栈溢出问题-仅供参考-问题是Unity现在需要添加一个元数据项