存档

2014年6月 的存档

Unity3d MeshRender

2014年6月15日 没有评论

I’m trying to use C# to disable and enable the MeshRender component in Unity3d however I am getting the following error,

error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)’

The line of code I am using is below. I’m using this in the same function.

MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>();

Also I’m posting here rather than Unity Answers as I get a far faster response here and it’s always useful information regardless of the outcome.

Look documentation:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        renderer.enabled = false;
    }
}

Link: http://docs.unity3d.com/ScriptReference/Renderer-enabled.html
Changing of programming languages upper-right.

You’re having trouble with several problems. First, you are trying to use GetComponent<> on a class instead of an instance of an object. This leads directly to your second problem. After searching for a specific GameObject you’re not using the result and you’re trying to disable the renderer of the GameObject containing the script. Third, C# is case-sensitive, Renderer is a class while renderer is a reference to an instance of Renderer attached to the GameObject

This code snippet combines everything: find the GameObject and disable its renderer

GameObject go = GameObject.FindWithTag("zone1");
if (go != null) { // the result could be null if no matching GameObject is found
  go.renderer.enabled = false;
}

You could use go.GetComponent<MeshRenderer>().enabled = false; instead of go.renderer. enabled = false; But by using renderer you don’t need to know what kind of renderer is used by the GameObject. It could be a MeshRenderer or a SpriteRenderer for example, renderer always points to the renderer used by the GameObject, if there exists one.

My friend. Just try use lowercase gameObject instead of GameObject and renderer instead of Renderer

The main problem that you try access Static class variable, using the name of class instead of class instance.
Class names here are GameObject and Renderer
And instances are gameObject and renderer

MeshRenderer showZone = GetComponent<MeshRenderer>();

delete the ‘GameObject.’

GameObject is a type. What you want is in an instance of a gameObject to call GetcComponent on. Thats what the error is about.

Which for note, this:

MeshRenderer showZone = GetComponent<MeshRenderer>();

is the exact same as this:

MeshRenderer showZone = this.GetComponent<MeshRenderer>();

You are calling GetComponent on the GameObject instance of which the script is attached to.

2 ways you can solve the Problem either. You add the word static to the method that is calling your statement.

ex :
public static GetTheMesh(){}

I do not recommend on doing this cause. If you have other calls inside the method that needs to access Instance, this will cause you trouble.

Second way of fixing it is make a pointer or reference first before getting the component. Or use the GameObject.Find <= which is slow if.

showZone = GameObject.Find("TheGameObjectName").GetComponent<MeshRenderer>();
分类: stackoverflow精选, unity3d 标签:

Android签名用keytool和jarsigner制作apk文件

2014年6月12日 没有评论

 

生成证书
keytool -genkey -alias aeo_android.keystore -keyalg RSA -validity 20000 -keystore aeo_android.keystore

签名
jarsigner.exe -verbose -keystore aeo_android.keystore -signedjar soccer-release.apk soccer-release-unsigned.apk aeo_android.keystore

验证签名
jarsigner.exe -verify soccer-release.apk

 备注 

同样的程序发布过程,在同事的机器上(JDK 1.6)签名后可以正常安装,但是在我机器上(JDK 1.7)签名后安装说签名错误无法安装。

找到了解决的办法,需要在签名程序 jarsigner 增加如下参数

-digestalg SHA1 -sigalg MD5withRSA

 

https://github.com/mobileresearch/weibo_android_sdk/blob/master/app_signatures.apk

另外微博和微信的app校验签名工具,实际对比的是程序签名和包名,包名在AndroidManifest.xml:

package=”” 

 

分类: cocos2d 标签:

How to include Cocos2dx UI library in Android project

2014年6月8日 没有评论

I’m trying to include one of the UI classes in my cocos2dx project.

In my scene class I have:

 #include "CocosGUI.h"

It compiles fine for iOS, but when I try to use the command line to compile for Android I get the following error:

fatal error: CocosGUI.h: No such file or directory

I understand it’s because it can’t find the library. I’ve been trying to find the right one to add but haven’t had any luck.

I’ve added:

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_ui_static

and

$(call import-module,extensions)
$(call import-module,ui)

But that doesn’t seem to help.

I’m using cocos2dx v3.0

I solved the problem by getting the right libraries to compile in the make file.

Here is what I added:

LOCAL_C_INCLUDES := ... /
$(LOCAL_PATH)/../../cocos2d/cocos/ui /

AND ADDING THESE LINES IN THE RIGHT SECTIONS

LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static
$(call import-module,editor-support/cocostudio)

Change your import to “ui/CocosGUI.h”. I am using 3.3.

You don’t need to edit your Android.mk’s import-module or import-add-path lines, or your build-cfg.json, all you need to do is change the specific headers to this generic one.

分类: cocos2d, stackoverflow精选 标签:

What is Mono Behaviour?

2014年6月6日 没有评论

I’m getting this message multiple times when I try to run my game:

The referenced script on this Behaviour is missing

For some reason, all my prefabs are missing a script that dictates something called their ‘Mono Behaviour':

What is Mono Behaviour?

What is the Mono Behaviour component, and what does it do?

How can I fix this error? I can click the little target circle to bring up all my scripts, but that just gives me all of the scripts that I’ve written:

What is Mono Behaviour?

So I don’t really know how to replace my missing Mono Behaviour script… Maybe if I knew what it was I could have a clue how to fix it.

I probably moved something. But how can I select the Mono Behaviour script (whatever it is) if the target just shows me my scripts?

Update

I’ve moved some assets in the project window, but not via the OS (Finder/Explorer) so why is this happening?

Also I am told that Mono Behaviour is a script I’ve applied…

Don’t really get this…isn’t the script component a reference to a script?

What is Mono Behaviour?

This happens when you have a script attached to a component, but then the script has been deleted (or its reference has lost in another way, such as the meta file being modified).

If you don’t know which script used to be there, just delete those Mono Behaviour components.

MonoBehaviour is the Base Class of all scripts that you want to attach to a GameObject. I am not sure how you managed to create a MonoBehaviour Component with a script missing. What you normally do is write your script and then just drag it from the project view onto the GameObject you want it attached to.

分类: cocos2d, stackoverflow精选 标签:

UnityEngine.WWW.WWW(string, byte[], System.Collections.Hashtable)' is obsolete

2014年6月5日 没有评论

I got an obsolete warning after updating to Unity 4.5 :

Warning CS0618: UnityEngine.WWW.WWW(string, byte[], System.Collections.Hashtable)' is obsolete:This overload is deprecated. Use the one with Dictionary argument.’ (CS0618) (Assembly-CSharp)

The codes are as follow:

public class Request {
    public string url;
    public NetworkDelegate del;
    public WWWForm form;
    public byte[] bytes;
    public Hashtable header;

    // Constructors
    public Request(string url, NetworkDelegate del) {
        this.url = url;
        this.del = del;
    }

    public Request(string url, NetworkDelegate del, WWWForm form) : this(url, del) {
        this.form = form;
    }

    public Request(string url, NetworkDelegate del, byte[] bytes) : this(url, del) {
        this.bytes = bytes;
    }

    public Request(string url, NetworkDelegate del, byte[] bytes, Hashtable header) : this(url, del, bytes) {
        this.header = header;
    }

    public WWW makeWWW() {
        if(header != null) {
            return new WWW(url, bytes, header); // problematic line
        }
        if(bytes != null) {
            return new WWW(url, bytes);
        }
        if(form != null) {
            return new WWW(url, form);
        }
        return new WWW(url);
    }
}

How should I change the line?

The original codes can be found here.

WWW constructor is not expecting an Hashtable anymore, but a Dictionary (its generic equivalent)

Do as the warning says : replace your Hashtable header member with a Dictionary<K,V> , K being the type of the Keys in the table, V the type of the values.

EDIT:

Also see why is Dictionary preferred over hashtable

分类: stackoverflow精选, unity3d 标签:

[Cocos2d-JS] 安卓机器的几个按钮

2014年6月3日 没有评论
cc.eventManager.addListener({
            event:cc.EventListener.KEYBOARD,
            onKeyPressed:function(keycode,event){},//无效 jsb未实现
            onKeyReleased:function(keycode,event){
                if(keycode==8){//back button 8
                    
                }else if(keycode==0x1067){//menu 4199
                    cc.Director.getInstance().end();
                }
            }
        },this);

 

分类: cocos2d 标签:

Swift基础用法(Swift开发之一)

2014年6月3日 没有评论

昨晚苹果发布了新一代编程语言Swift,官方提供了一个iBook的说明文档,有需要的可以看下。地址:https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11

下面我先说下基本的一些东西,大家都是刚看,所以我也难免会出现理解错误的地方。欢迎指出。当然你也可以直接看官方的文档。

一、let,var基本类型

let:常量,不能二次赋值。赋值时可以不需要指定类型,编译器根据赋值自动判断。也可以手工指明类型。
var:变量

let a = 12 // 申明a为常量12,类型Int
let b: Double = 12 // 申明a为常量12,手工指定类型Double

二、字符串

1、加号可以直接拼接字符串,不能直接拼接其他类型转化成string
 

let str = "Hello, playground. "
let name = "Tom "
let count = 10
let say = str + name + String(count)  // "Hello, playground. Tom 10"

2、 字符串中直接引入代码

let a = 1
let b = 2
let say = "the number is /(a + b)"  // "the number is 3"

三、数组、字典 都可以直接使用 [] 声明

1、数组

var arr = ["catfish", "water", "tulips", "blue paint"]
arr[1] = "bottle of water" // 修改第二项
arr // ["catfish", "bottle of water", "tulips", "blue paint"]

2、字典

var man = [
    "like": "apple",
    "name": "Tom",
]
man["iphone"] = "5s"  // 增加字段
man["like"] = "mac"  // 修改字段
man // ["iphone": "5s", "like": "mac", "name": "Tom"]


四、控制语句

1、if  不可以直接使用一个变量或者常量来作为条件,必须是判断语句

let a = 12
if a > 0 {
    // do something
} else {
   // do something
}

如果你写成 if a {} ,就会报错,不能直接使用变量或常量判断bool

2、for

for i in 0..3 {
   // i = 0, 1, 2
}


for var i = 0; i < 3; ++i {
    // i = 0, 1, 2
}


// 数组
let scores = [1, 2, 3, 4, 5]
for score in scores {
    // score 
}


// 字典
let numsDic = [
    "a": [2, 3, 5, 7, 11, 13],
    "b": [1, 1, 2, 3, 5, 8],
    "c": [1, 4, 9, 16, 25],
]
for (key, numbers) in numsDic {
    for number in numbers {
        // number
    }
}

3、while

var n = 2
while n < 100 {
    n = n * 2
}


var m = 2
do {
    m = m * 2
} while m < 100
分类: 未分类 标签:

游戏开场镜头拉近(Unity3D开发之四)

2014年6月1日 没有评论

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=553

今天看了个Demo,发现它的游戏开场会先有个白色遮罩层的渐变消失,然后镜头拉进到目标场景。镜头拉进主要用Vector3.MoveTowards()

游戏开场镜头拉近(Unity3D开发之四)

1、添加一个GUITexture,用作白色遮罩层。设置Active false,事先不可见。不然就挡住你场景了。
2、添加一个EmptyObject,放置在最终希望camera放置的位置。
3、将camera position.z 调整到离目标位置的合适距离。

using UnityEngine;
using System.Collections;

public class DemoScene : MonoBehaviour {
	
	public Transform targetPoint; // camera将要移动到的目的地,可以是一个EmptyObject
	public GameObject titleText, whiteMask; //title, 白色的全屏遮罩 (事先在场景中Active为false)

	private int pattern = 0;
	private float alpha = 0.5f;
	
	// Use this for initialization
	void Start () {
		alpha = 0.5f;
		whiteMask.SetActive(true);
	}
	
	// Update is called once per frame
	void Update () {
		
		// 移动camera到指定的地点
		transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, 3 * Time.deltaTime);	
		
		if(pattern == 0){
			// 慢慢降低白色遮罩层的透明度
			if(alpha > 0){
				alpha -= Time.deltaTime * 0.2f;
    			whiteMask.guiTexture.color = new Color(.5f,.5f,.5f, alpha);
			}

		    // 当接近目标点的时候,显示游戏title
			if(Mathf.Abs(transform.position.z - targetPoint.position.z) <= 6.0f){
				pattern = 1;
				alpha = 0;
				titleText.SetActive(true);
			}
		}
		
		if(pattern == 1){
			// 慢慢增加透明度,显示title logo
			if(alpha < 0.5f){
				alpha += Time.deltaTime * 0.5f;
    			titleText.guiTexture.color = new Color(.5f,.5f,.5f, alpha);
			}else{
				pattern = 2;
			}
		}
		
		if(pattern == 2){	
			if(Input.anyKey){
				// 处理后续的事
			}
		}
	}
}

分类: 未分类 标签: