新手学unity,unity中c#脚本问题,求高手帮助

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新手学unity,unity中c#脚本问题,求高手帮助相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
string str;
Texture imageTexture;
private int imageWidth;
private int imageHeight;
private int screenWidth;
private int screenHeight;
// Use this for initialization
void Start ()
screenWidth=Screen.width;
screenHeight=Screen.height;
imageWidth=imageTexture.width;
imageHeight=imageTexture.height;


// Update is called once per frame
void OnGUI ()
UnityEngine.GUI.Label(new Rect(100,10,100,30),str);
UnityEngine.GUI.Label(new Rect(100,40,100,30),"当前屏幕宽:0",screenWidth);
UnityEngine.GUI.Label(new Rect(100,80,100,30),"当前屏幕高:0",screenHeight);
UnityEngine.GUI.Label(new Rect(100,120,0,0),imageWidth,imageHeight,imageTexture);




错误提示Assets/NewBehaviourScript.cs(22,33): error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string, UnityEngine.GUIStyle)' has some invalid arguments
Assets/NewBehaviourScript.cs(22,33): error CS1503: Argument `#3' cannot convert `int' expression to type `UnityEngine.GUIStyle'
Assets/NewBehaviourScript.cs(23,33): error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string, UnityEngine.GUIStyle)' has some invalid arguments
Assets/NewBehaviourScript.cs(23,33): error CS1503: Argument `#3' cannot convert `int' expression to type `UnityEngine.GUIStyle'
Assets/NewBehaviourScript.cs(24,33): error CS1501: No overload for method `Label' takes `4' arguments

参考技术A 函数重载方法错了 ,给你官方的脚本手册关于这个函数的
static function Label (position : Rect, text : string) : void
static
function Label (position : Rect, image : Texture) : void
static function
Label (position : Rect,
content : GUIContent) :
void
static function Label (position : Rect, text : string, style : GUIStyle) : void
static function
Label (position : Rect,
image : Texture, style :
GUIStyle) : void
static function
Label (position : Rect,
content : GUIContent,
style : GUIStyle) : void
它只有这几种重载形式……再给你说下各个参数的意思

position
标签使用的在屏幕上矩形的位置

text
在标签上显示的文本

image
在标签上显示的纹理

content
在标签上显示的文本,图片和信息提示

style
使用样式,如果不使用那么,标签样式应用当前的GUISKin皮肤。
希望能帮到你追问

static function Label()这个在java和c#是一样的吗?
你能不能在我的基础上帮我改改

本回答被提问者和网友采纳

从 Android 调用 C# Unity 脚本

【中文标题】从 Android 调用 C# Unity 脚本【英文标题】:Calling C# Unity Script from Android 【发布时间】:2013-09-11 14:57:11 【问题描述】:

我一直在尝试使用我的 Android 应用程序与我的 Unity C# 脚本进行通信 "UnityPlayer.UnitySendMessage("Cube", "Test","HELLO") - 其中“Cube”是我的 Unity 对象,“Test”是它正在使用的 C# 脚本中存在的方法的名称,“HELLO”是我要传递的消息字符串。

这一行放在我的onClick函数中,像这样:

      ImageButton right_button = (ImageButton) findViewById(R.id.right_arrow);
      right_button.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View v) 
            // TODO Auto-generated method stub
            UnityPlayer.UnitySendMessage("Cube", "Test","HELLO");
       
      );

但是当我运行我的 android 应用程序并单击此按钮时,我会收到此错误:

    09-11 14:22:37.526: E/AndroidRuntime(1330): java.lang.NoClassDefFoundError:         com.unity3d.player.UnityPlayer

我的构建路径中也包含了 classes.jar。 我还有什么遗漏的吗?

提前致谢!

【问题讨论】:

找不到你的UnityPlayer类。您还期望得到什么答案。 这有帮助吗? answers.unity3d.com/questions/279581/… 我正在使用 Eclipse IDE。我设法通过将我的 classes.jar 放在 libs 文件夹中并从那里引用它来摆脱上述错误,但现在我收到此错误:09-11 19:17:36.339: E/AndroidRuntime(930): Caused by: java .lang.UnsatisfiedLinkError:未找到本机方法:com.unity3d.player.UnityPlayer.UnitySendMessage:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;) 【参考方案1】:

检查 class.jar 文件是否在项目的构建路径中。如果是,请检查它是否被选中(已标记复选框)。

提示:如果您遇到 ClassDefNotFound 运行时错误(或类似错误),您可能需要执行此额外步骤:

转到 Project > Properties > Java Build Path -> Order and Export 选项卡并检查(勾选)classes.jar、Android x.y 和 Android Dependencies 项;然后应用并重建 probject。

【讨论】:

【参考方案2】:

上次也发生在我身上。 我清理、重新链接并重建我的 Eclipse 项目。将我的 jar 文件重新导出到 unity 并重新启动 Unity。再次构建,错误消失了。

【讨论】:

以上是关于新手学unity,unity中c#脚本问题,求高手帮助的主要内容,如果未能解决你的问题,请参考以下文章

新手如何从零开始学习unity?

(C#脚本)Unity中角色移动的几种实现方法,以及心得。(适合新手入门)

用Unity3D开发赛车游戏碰到的几个问题,求高手解答

问一个简单的问题,用unity3d自己做游戏(业余),脚本方面需要学习c#的哪些方面,只学必要的,

Unity 不会在任何组件和任何项目中运行我的 c# 脚本。它虽然读取所有值

unity中两脚本间怎么传值呢?