4.unity3D 预设的一例

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.UI;

public class TestClick : MonoBehaviour {

public static GameObject my;
// Use this for initialization
void Start () {

// 1.在打开 程序的时候 去查找 这个 button  如果没有找到就新建一个 button
GameObject btnObj = GameObject.Find("Buttonx");
if (btnObj == null) { //如果 btn 没有
my = GameObject.Find ("New Prefab");  // 找到这个 button 的预设
GameObject can = GameObject.Find ("Canvas"); // 找到 canvas 
GameObject go = Instantiate (my, new Vector3 (260, -70, 0), Quaternion.identity); //新建一个 预设 在位置 260,-70
go.name = "Buttonx"; // name 的实例名
go.transform.SetParent (can.transform, false); 把 这个预设加到  Canvas上面.
Debug.Log ("create!");
}
}
public void Click(){
Debug.Log ("Button 134 Clicked. TestClick.");
GameObject btnObj = GameObject.Find("Buttonx");

Button btn = btnObj.GetComponent<Button>();

btn.interactable = false;
// 这里演示怎样删除这个 button ,
GameObject.Destroy (btnObj);
//btn.enabled = false;
GameObject txtObj = GameObject.Find ("Textx");
Text txt = txtObj.GetComponent<Text>();
txt.text="夏静12345";
//Application.Quit();

}
public void Click2(){
GameObject btnObj = GameObject.Find("Buttonx");
if (btnObj == null) {
//GameObject.Instantiate(my,new Vector3(10,10,0),Quaternion.identity);
GameObject can = GameObject.Find ("Canvas");
// 这段代码增加到 can上了,但没有显示
//GameObject buttonadd = new GameObject("buttonadd");
//buttonadd.AddComponent <Button>();
//buttonadd.transform.parent = can.transform;

//
my = GameObject.Find("New Prefab");
//Debug.Log ("abc:"+my.GetType ().ToString ());
//my.
//GameObject buttonPrefab = new GameObject("buttonPrefab"); // 这个是对象的名称
//Text tx =buttonPrefab.AddComponent <Text>();
//tx.font = new Font ("Arial");
//tx.fontStyle = FontStyle.Normal;
//tx.text="add";
//buttonPrefab.transform.SetParent (can.transform,false);

GameObject go = Instantiate (my, new Vector3 (260, -70, 0), Quaternion.identity);
go.name = "Buttonx";
//Debug.Log (go.name);
go.transform.SetParent (can.transform,false);
//这个还不知道是什么意思
//GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//cube.AddComponent<Rigidbody>();
Debug.Log ("btnObj is null");
} else {

}
}
// Update is called once per frame
void Update () {

}
}

时间: 2024-08-04 18:20:16

4.unity3D 预设的一例的相关文章

Unity3D 预设打包的注意事项

在平时的开发中,把预设打包成 assetbundle 文件是非常普遍的做法,但是我们不能随便把预设打包成 assetbundle 就算完事,我们应该先清楚把预设打包成 assetbundle 的目的,通常我们把预设打包成 assetbundle 文件是为了压缩预设的文件尺寸以方便从网络快速加载,既然这样,我们就应该知道打包预设的时候应该注意哪些问题. 我们先创建测试环境,场景中的 Stamp7 与 Stamp9 网格不同,但是材质相同,R1_Column1 与 Stamp7 以及 Stamp9

一、Unity3D 5.0.1-示例项目“汽车游戏”的展示及开发准备

学习之前首先给大家提供一下Unity3D 5.0.1的各操作系统的安装文件及示例项目等下载地址: Win版: 32位: http://netstorage.unity3d.com/unity/5a2e8fe35a68/Windows32EditorInstaller/UnitySetup32.exe 链接:http://pan.baidu.com/s/1mgBpsLi 密码:x548 64位: http://netstorage.unity3d.com/unity/5a2e8fe35a68/Wi

Unity3d Web Player 的server端联网配置

http://blog.csdn.net/dyllove98/article/details/9501695 新游戏出了第一个能跑完流程的版本,不得不佩服Unity3D强大的功力,PC.MAC OS.Linux.IOS.Android.web player,前天刚发布的unity3d 4.2版本还支持WINDOWS PHONE.Black Barry这些平台统统一键打包,so easy!不过在打包部署Web Player时,在联网方面出现了一点麻烦,算是顺风顺水过程当中的一个小插曲吧!所以又可以

从游戏脚本语言说起,剖析Mono所搭建的脚本基础

0x00 前言 在日常的工作中,我偶尔能遇到这样的问题:“为何游戏脚本在现在的游戏开发中变得不可或缺?”.那么这周我就写篇文章从游戏脚本聊起,分析一下游戏脚本因何出现,而mono又能提供怎样的脚本基础.最后会通过模拟Unity3D游戏引擎中的脚本功能,将Mono运行时嵌入到一个非托管(C/C++)程序中,实现脚本语言和“引擎”之间的分离. 回到目录 0x01 Why?从为何需要游戏脚本开始 首先聊聊为何现在的游戏开发需要使用游戏脚本这个话题. 为何需要有脚本系统呢?脚本系统又是因何而出现的呢?其

linux之SELinux

SELinux 概述:SELinux 全称Security Enhanced Linux(安全性增强的Linux),由美国国家安全局NSA(National Security Agency)开发,构建亍kernel 乀上的.拥有灵活而强制性的访问控制结构,旨在提高Linux 系统的安全性,提供强健的安全保证,可防御未知攻击,据称相当亍B1级的军事安全性能:已经被整合到2.6 以上kernel 中:传统Linux 在没有SELinux 保护的时候,倘若运行亍乀上的服务被骇客攻陷,其服务器最高权限就

Linux系统管理之十五---shell

一.shell基础1.shell的基本概念shell就是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具.用户界面shell(还有其他用户界面如kde等图形界面)-->内核-->硬件. 2.shell的种类/bin/sh/bin/bash  默认shell/bin/ksh   兼容bash/bin/tcsh  c shell/bin/csh   已被tcsh替代,c shell可以在/etc/shells文件中查看有哪些shell种类, 以下我们要学的都是bash这个shell的知识

【原创】我所理解的资源加载方式

最近转战unity3d,接的第一个任务就是关于资源管理的部分. 由于项目是web和standalone(微端)并存的,所以希望保证业务逻辑尽量保持一致,跟之前ios,android的执行流程略有不同,比如在web模式下,FILE类是被禁用的,所以指望通过写文件来操作相关功能参数的方法是不可行的.下面,是我对这方面的一些理解.本文中如果没有特殊说明,下载的资源都是AssetBundle web程序的运行流程大致是 首先加载web.html,这是整个程序的入口 他会加载相关的unity3d文件(本例

Unity3d学习 预设体(prefab)的一些理解

之前一直在想如果要在Unity3d上创建很多个具有相同结构的对象,是如何做的,后来查了相关资料发现预设体可以解决这个问题! 预设体的概念: 组件的集合体 , 预制物体可以实例化成游戏对象. 创建预设体的作用: 可以重复的创建具有相同结构的游戏对象. .1下面来讲解一下如何制作一个简单的预设体(上下为流程和结果图): .2 创建多个prefabs_new(代码,结果图): 1 using UnityEngine; 2 using System.Collections; 3 4 //[Execute

游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)

(转)游戏引擎中三大及时光照渲染方法介绍(以unity3d为例) 重要:在目前市面上常见的游戏引擎中,主要采用以下三种灯光实现方式: 顶点照明渲染路径细节 Vertex Lit Rendering Path Details 正向渲染路径细节 Forward Rendering Path Details 延迟光照渲染路径的细节 Deferred Lighting Rendering Path Details 以unity3d为例,以下将详细讲解三种灯光渲染方式的实现.原理及缺陷. 顶点照明渲染路径