unity 导入 obj 模型 和 json 数据

首先 弄了一个导出 obj 模型的脚本 链接如下:

链接:https://pan.baidu.com/s/1rqEyM4RxZEApJkKPKhu3Ng 
提取码:fq4t

把该脚本存放在 Editor/Scripts 文件夹下  就可以把模型导出成为 obj 格式

操作:选中物体 custom--> Export --> Export whole selection to single Obj 

结果:在项目文件夹里,有一个 ExportedObj 文件夹 所产生的 obj 文件 全在里面存放

obj文件的数据 用json文件记载 格式如下:


  "atlasOption": {
    "maxSize": 2048,
    "pixelPerUnit": 10
  },
  "geometrics": [
    {
      "obj": "1.obj",
      "instanceNum": 6
    },
    {
      "obj": "2.obj",
      "instanceNum": 7
    },
    {
      "obj": "3.obj",
      "instanceNum": 10
    }
  ]
}

用 C# 脚本对 json 读取类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;

public class JSONLoader
{
    private Root root;

    public Root load(string path)
    {
        path = Path.Combine(path, "input.json");

        StreamReader streamreader = new StreamReader(path);    // 读取数据 转换成数据流

        JsonReader js = new JsonReader(streamreader);    // 再转换成json数据

        this.root = JsonMapper.ToObject<Root>(js);    // 读取

        return this.root;
    }
}

 其 记录 json 的数据结构:

using System.Collections.Generic;

public class AtlasOption
{
    /// <summary>
    /// 
    /// </summary>
    public int maxSize { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int pixelPerUnit { get; set; }
}

public class GeometricsItem
{
    /// <summary>
    /// 
    /// </summary>
    public string obj { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int instanceNum { get; set; }
}

public class Root
{
    /// <summary>
    /// 
    /// </summary>
    public AtlasOption atlasOption { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public List<GeometricsItem> geometrics { get; set; }
}

 实现对 外部 obj json 进行 文件转移 到 Assets/Resources 目录下:

using UnityEngine;
using UnityEditor;
using System.IO;
using System;
public static void loadModel()
    {
        //读取
        string path = EditorUtility.OpenFolderPanel("载入模型文件夹", "", "");
        JSONLoader loader = new JSONLoader();
        
        //读取JSON文件
        root = loader.load(path);
        
        // 目标文件夹
        string targetPath = Application.dataPath + "/Resources";
        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);

        // 对每个 geometrics 进行 文件传输
        for (int i = 0; i < root.geometrics.Count; i++)
        {
            // 文件源
            string fileName = path + "/" + root.geometrics[i].obj;
            FileInfo file = new FileInfo(fileName);
            
            // 目标文件 路径
            string targetFilePath = targetPath + "/" + root.geometrics[i].obj;

            // 文件传输 
            file.CopyTo(targetFilePath, true);
        }


    }

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值