Unity3d- 资源
Data与Resources文件夹
一般只读文件放到Resources目录
Data用于新建的文件或者要修改的文件
=================================================================
Unity默认的Resource加载
保存和读取资源的 最简单的 方式了。但是 如果想让资源在编译后不改变,只能这样做。
因为这里面的内容会 组合并编译到resource.assets文件(此文件可以在编译过的游戏的Data文件中找到)
可以从编译过的项目文件Resource.assets中去加载
用法
//Texture2D (Texture2D)Resouce.Load(fileName);
//文本资源 (TextAsset)Resouce.Load("ddd");
读取文本内容 TestAsset.text
//声音 (AudioClip)Resouce.Load();
==========================================================
加载编译之后的文件
using System.IO;
using UnityEngine;
using System.Collections; public class temp : MonoBehaviour
{
private string fileName = "a.jpg";
private string url;
private Texture2D externalImage;
// Use this for initialization
void Start()
{
url = "file:" + Application.dataPath;
url = Path.Combine(url, "Resources");
url = Path.Combine(url, fileName); StartCoroutine(LoadWWW());
} private IEnumerator LoadWWW()
{
yield return ;
WWW www=new WWW(url);
yield return www;
externalImage = www.texture;
}
// Update is called once per frame
void Update()
{ }
}
需要System.IO类
www类的对象时以“file:"开头的URL,
使用Path.Combine()而不是 ”\ /“以避免跨平台的问题
WWW和资源内容
WWW类定义了几个不同的属性和方法 用于把下载的资源 文件 数据提取到变量中
.text 只读 web数据作为string类型返回
.texture 只读 作为Texture2D类型返回
.GetAudioClip() 方法 作为AudioClip对象类型返回
=======================================================
通过从互联网 下载文件与加载外部资源文件
从服务器下载
using UnityEngine;
using System.Collections; public class temp : MonoBehaviour
{
private string url="http://www.packtpub.com/sites/default/files/packt_logo.png";
private Texture2D externalImage;
// Use this for initialization
void Start()
{
StartCoroutine(LoadWWW());
} private void OnGUI()
{
GUILayout.Label("url="+url);
GUILayout.Label(externalImage);
}
private IEnumerator LoadWWW()
{
yield return ;
WWW imageFile=new WWW(url);
yield return imageFile;
externalImage = imageFile.texture;
}
// Update is called once per frame
void Update()
{ }
}
==========================================================
使用外部文本文件和XML数据
最简单的是TextAsset 共有变量(编译后数据文件不再改变的情况下适用)
public TextAsset DataTestAsset;
private string textData; void Start()
{
textData = DataTestAsset.text;
}
C#文件流加载外部文本文件
using System;
using UnityEngine;
using System.Collections;
using System.IO;
public class FileReadWriteManager
{
public void WriteTextFile(string pathAndName, string stringData)
{
//remove file ,if exists
FileInfo textFile=new FileInfo(pathAndName);
if (textFile.Exists)
{
textFile.Delete();
}
//create new empty file
StreamWriter writer;
writer = textFile.CreateText();
//write text to file
writer.Write(stringData);
writer.Close();
} public string ReadTextFile(string pathAndName)
{
string dataAsString = "";
try
{
//open text file
StreamReader textReader = File.OpenText(pathAndName);
//read content
dataAsString = textReader.ReadToEnd();
textReader.Close();
}
catch (Exception e)
{ }
return dataAsString;
}
}
调用示例
private string fileName = "cities.txt";
private string filePath = "";
private FileReadWriteManager fileReadWriteManager=new FileReadWriteManager();
void Start()
{
filePath = Path.Combine(Application.dataPath, "Resources");
filePath = Path.Combine(filePath, fileName);
string textContent = fileReadWriteManager.ReadTextFile(filePath);
}
private string fileName = "hello.txt";
private string foleName = "Data";
//需要在Project面板中创建一个Data目录文件夹
private string filePath = "";
private FileReadWriteManager fileReadWriteManager=new FileReadWriteManager();
void Start()
{
filePath = Application.dataPath + Path.DirectorySeparatorChar + fileName;
string textData = "abc\n def"; fileReadWriteManager.WriteTextFile(filePath, textData);
filePath = Path.Combine(filePath, fileName);
string textContent = fileReadWriteManager.ReadTextFile(filePath);
}
Unity3d- 资源的更多相关文章
- unity3d 资源文件从MAX或者MAYA中导出的注意事项
unity3d 资源文件从MAX或者MAYA中导出的注意事项 1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择: 1) 导出资源时,只导出一个文件,保留模型,骨骼和所 ...
- KEngine:Unity3D资源的打包、加载、调试监控
资源模块做什么? 资源模块——ResourceModule,是KEngine中最核心的模块,其他模块基本或多或少的对它有依赖,它主要的功能是:资源打包.路径定义.资源管理.资源调试. 资源模块对Uni ...
- Unity3D资源存放笔记
文件夹及路径 昨天记了一篇AssetBundle学习笔记,那么游戏中的各种资源应该如何存放呢? 在网上一阵搜罗,把笔记记一下. 非特殊名称文件夹 非Unity3D指定名称的文件夹中的资源,如果游戏场景 ...
- unity3d 资源打包加密 整理
资源打包脚本,放到Assets\Editor 文件夹下 using UnityEngine; using System.Collections; using UnityEditor; using Sy ...
- 【转】unity3d 资源文件从MAX或者MAYA中导出的注意事项
转自游戏开发主席 1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择: 1) 导出资源时,只导出一个文件,保留模型,骨骼和所有的动作帧(把所有的动作,比如idle,atta ...
- unity3d资源打包总结
http://www.manew.com/blog-33734-12973.html unity 打包的时候会把下面几个文件资源打进apk或者ipa包里面 1. Asset下的所有脚本文件 2. As ...
- Unity3D资源
1.ShareSDK 地址:https://github.com/MobClub/New-Unity-For-ShareSDK 文档:产品集成步骤 2.Protobuf https://github. ...
- [vuforia][unity3d]资源链接
http://bbs.csdn.net/topics/390787189 CSDN论坛中 “Qualcomm Vuforia(AR虚拟现实)开发” 主题资源下载 http://bbs.csdn.net ...
- (转)unity3D 如何提取游戏资源 (反编译)+代码反编译
原帖:http://bbs.9ria.com/thread-401140-1-1.html 首先感谢 雨松MOMO 的一篇帖子 教我们怎么提取 .ipa 中的游戏资源.教我们初步的破解unity3d资 ...
- (转)Unity3D 开发优秀技术资源汇总
原文:http://www.j2megame.com/html/xwzx/ty/3179.html Unity3D 博客 http://www.dapp.com.br/ by Dapp http:/ ...
随机推荐
- CF446 (Div. 1)简单题解
A .DZY Loves Sequences pro:给定长度为N的序列,你最多可以改变一个数的值,问最长严格上升子序列长度. N<1e5. sol:分几种情况,一种的不改变: 一种是改变,然后 ...
- EasyUI datagrid 格式 二
单击保存,改表的状态 { field: 'ck', checkbox: true }, $("tr").each(function () { if ($(this).find(&q ...
- vue的理解
vue提供的MVVM框架模式的数据双向绑定,实现了HTML和js的代码分离,提高代码的维护性 vue.js的核心思想包括:数据驱动和组件化思想. 如果没有中间的ViewModel则关系图编程下面所示: ...
- THML文档布局元素
学习要点: 1.文档元素总汇 2.文档元素解析 一.文档元素总汇 文档元素基本没有什么实际作用效果,主要目的是在页面布局时区分各个主题和概念. 元素名称 ...
- hdu1907 John 博弈
Little John is playing very funny game with his younger brother. There is one big box filled with M& ...
- wekpack笔记
1. webpack 是一个用来构建我们应用程序中的 JavaScript 模块的工具: 2. 可以从CLI 或 API来开始使用 webpack.这里只讲从CLI来使用它: 3. 安装,需要在nod ...
- LeetCode - Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
- JQuery左右切换实现
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 收集的dubbo博客
1.http://shiyanjun.cn/archives/category/opensource/dubbo 2.https://blog.csdn.net/hellozpc/article/de ...
- 用flask和长轮询实现对帅哥投票和实时查看票数
flask中的代码 from flask import Flask,request,render_template,redirect,session,jsonify import uuid impor ...