SQL语句 协同程序 WWW VideoPlayer组件

SQL语句封装

using UnityEngine;
using Mono.Data.Sqlite;
using System.Collections.Generic;
using System.Collections;

public class SQLFramework {

	#region 单利
	private static SQLFramework instance;
	
	public static SQLFramework GetInstance(){
        if(instance == null){
			instance = new SQLFramework();
        }
        return instance;
	}
	
	private SQLFramework(){
	}
	#endregion
        
    #region 字段
	private SqliteConnection con;
    private SqliteCommand command;
    private SqliteDataReader reader;
    #endregion
        
    #region SQL框架
    //打开数据库
    public void OpenDatabase(string dbName){
        //如果不是以.sqlite
        //动态添加后缀
        if(!dbName.EndsWith(".sqlite")){
            dbName += ".sqlite";
        }
        //数据库路径
        string dbPath = "";
        //预编译指令:在unity中运行或在PC运行
        #if UNITY_EDITOR || UNITY_STANDALONE
             dbPath = "Data Source = " + Application.streamingAssetsPath + "/" + dbName;
        #endif
        //实例化连接对象
		con = new SqliteConnection(dbPath);
        //打开连接
        con.Open();
        //实例化指令对象
        command = con.CreateCommand();
    }
	
    //关闭数据库
    public void CloseDatabase(){
        //清堆内存
        con.Close();
        command.Dispose();
        //reader.Close();
        //清栈内存
        con = null;
        command = null;
        //reader = null;
    }
    
    //仅仅执行SQL语句
	public int JustExecute(string sqlQuery){
        //设置SQL语句
        command.CommandText = sqlQuery;
        //执行SQL语句
        return command.ExecuteNonQuery();
    }
    //查 一个
    public object SelectSingleData(string sqlQuery){
        //设置SQL语句
        command.CommandText = sqlQuery;
        //执行SQL语句
		return command.ExecuteScalar();
    }
    //查 多个
	public List<ArrayList> SelectMultipleData(string sqlQuery){
        //设置SQL语句
        command.CommandText = sqlQuery;
        //执行SQL语句
		reader = command.ExecuteReader();
        //创建List<ArrayList>
        List<ArrayList> result = new List<ArrayList>();
        
        while(reader.Read()){
            //新建ArrayList 存储数据
            ArrayList rowData = new ArrayList();
            //所有列
            for(int i = 0; i < reader.FieldCount; i++){
                //将当前行,当前列的数据添加
                rowData.Add(reader.GetValue(i));
            }
            //ArrayList 添加到 List
            result.Add(rowData);
        }
        //关闭读取器
        reader.Close();
        //返回结果
        return result;
    }
    #endregion    
}

补充一个知识点 : 单例类与单例脚本

单例类是 不继承MonoBehaviour

单例脚本 继承MonoBehaviour

协同程序

任何一个应用 就是一个程序 一般一个应用程序是一个进程

using UnityEngine;
using System.Collections;

public class IEDemo : MonoBehaviour {
	
    private void Start(){
        //启动方法
        //startCoroutine(Demo());
        
        startCoroutine(GenerateWave());
        
        //停止
        StopAllCoroutines();//这个方法尽量不要用  停止所有的协程
        
        StopCoroutines();
        
        
    }
    
    /*
	IEnumerator Demo(){
        while(true){
            Debug.Log("1");
            //yield return 0; // yield return 0也行1也行null也行 只要是具体的值 就等一帧  update之后
            //yield return new WaitForEndOfFrame(); 等一帧    OnGUI之后
            //yield return new WaitForSeconds(2); 隔几秒  
            Debug.Log("2");
        }
        
        while(true){
        	Debug.Log("1");
        	yield return new WaitForFixedUpdate(); //0.02秒
        }
        
	}
    */
    
    
    //yield return StartCoroutine
    //举个例子
    IEnumerator GenerateWave(){
        while(true){
            //等GenerateMonsters这个协程执行完
            yield return StartCoroutine(GenerateMonsters());
            Debug.Log("当前波次怪物生成完毕");
            yield return new WaitForSeconds(3);
        }
    }
    IEnumerator GenerateMonsters(){
        int count = 0;
        while(count < 10){
            Debug.Log("生成一个怪");
            count++;
            yield return new WaitForSeconds(1);
        }
    }
    
    
    
}

查找的办法:

(1)API 手册 Order

(2)API 手册 脚本 脚本概述 事件函数的执行顺序

using UnityEngine;
using System.Collections;

public class IEDemo : MonoBehaviour {
	
    private void Start(){
        //启动方法
        startCoroutine("Demo");
    }
    
	IEnumerator Demo(){
        
        int count = 0;
        
        while(true){
            if(count == 3)
            	yield break;  //跳出协程
        	Debug.Log("1");
            count++;
        	yield return new WaitForFixedUpdate(); //0.02秒
        }
	}
    
    private void Update(){
        //注意 停止协程用的方法要对应 
        //字符串的停止相对比其他两种更耗性能,底层通过反射实现
        if(Input.GetKeyDown(KeyCode.Space)){
             StopCoroutines("Demo");
        }
    }
}

WWW

用来下载网上资源或者服务器里的资源

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WWWDemo : MonoBehaviour{
    public Image image;
    
    private string textureURL = "这里边放的url";
    
    private IEnumerator Start(){
        yield return StartCoroutine(DownloadText());
        
    }
    //第二种加载方式 适合下载图片视频 可以显示下载进度
    IEnumerator DownloadTexture(){
        WWW www = new WWW(textureURL);
        //isDone下载完了
        while(!www.isDone){
            //下载进度
            Debug.Log(www.progress);
            yield return 0;
        }
        
        Texture2D texture2D = www.texture;
        //将图片贴到圆柱体上
        GetComponent<MeshRenderer>().material.mainTexture = texture2D;
        
        //将下好的图片转换成精灵赋给图片
        //sprite精灵									头两个坐标后两个大小  中心点
        image.sprite = Sprite.Create(texture2D,new Rect(0,0,texture2D.width,texture2D.height),new Vector2(0,0));
    }
    
    
    //第一种加载方式 放个文本啥的可以 也不大 下载完后可以用json解析出来
    IEnumerator DownloadText(){
        //string URL     <------URL 网址
        WWW www = new WWW("http://www.baidu.com");
        //等待下载完毕后恢复
        yield return www;  //白话翻译就是等这个www下载完了 在执行后面的
        Debug.Log(www.Text);
    }
}

Video Player 组件

Source 视频源
Video Clip
Play On Awake 游戏开始播放
Wait For First Frame 是否等一帧
Loop 循环
Playback Speed 播放速度
Render Mode 渲染模式 Camera Far Plane 摄像机的远景面 Camera Near Plane 近景面 Render Texture 视频放Raw Image里播放 Material Override 视频放材质球里播放 API Only
Alpha 透明度
Aspect Ratio 自适应
Audio Output Mode 音频播放

这种WWW下载方式中途不能断,关了重启的话需要重新下载,可以搜索其他的断电续传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值