By using this UI tool with Unity editor windows, allow you easily uploading user files under folders and/or selecting a single file one by one from your local storage, then transfer them to your app's global file system.
Let's get started.
Step 1: Create a global file folder from the brainCloud portal.
Go to brainCloud development portal Design | Custom Config | Global Files page, click
Create Folder
option fromAction
drop-down menu.
Note down the folder ID from the list, we will use it as the folder
guid
for storing the transferred user files later on. (eg,6f631d00-131b-4474-a08d-83dbc6ee7fa6
)
Step 2: Create a cloud code client script for calling files moving api.
Advance to Design | Cloud Code| Scripts page, create a client script with parameter as follows (replace the
foldertreeid
with the one you created from the above step), name it astransferUserFilesToGlobalFiles
or whatever name you want, just make sure to keep it the same as the one in Unity code.
{
"foldertreeid": "6f631d00-131b-4474-a08d-83dbc6ee7fa6"
}
Paste the code below to your main script.
"use strict";
function main() {
var response = {};
response.data = [];
var userCloudPath = "bulk_files";
var userProfileId = bridge.getProfileId();
var globalFileProxy = bridge.getGlobalFileV3ServiceProxy();
var fileProxy = bridge.getFileServiceProxy();
var postResult = fileProxy.listUserFiles(userCloudPath, true);
bridge.logDebugJson("files", postResult);
for (var i = 0; i < postResult.data.fileList.length; ++i)
{
response.data.push( globalFileProxy.sysMoveToGlobalFile(
userProfileId, userCloudPath, postResult.data.fileList[i].cloudFilename, data.foldertreeid, postResult.data.fileList[i].cloudFilename, true ));
}
return response;
}
main();
Step 3: Create a Unity editor window from your Unity app.
Open/create one of your Unity apps with Unity editor, assuming brainCloud Unity package is already imported to this app.
Create a script under
Assets
folder and paste the code below to your script. Replace thefoldertreeid
with the folder id you created from step 1. (from code line here:string foldertreeid = "6f631d00-131b-4474-a08d-83dbc6ee7fa6";
)
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace BCtools.Core.BrainCloud
{
[ExecuteInEditMode]
public class BCBulkUploadFiles : EditorWindow
{
[MenuItem("BCTools/Upload Files To App")]
static void Init()
{
GetWindow<BCBulkUploadFiles>("BCTools");
EditorApplication.hierarchyChanged += hierarchyWindowChanged;
GetBCClient();
}
private static BrainCloudWrapper _wrapper = null;
private Vector2 scroll;
private static string _debug = "";
private static string _path = "";
private static List<FileInfo> _pathToFilesToUpload = new List<FileInfo>();
private bool bCriticalState = false;
private void OnDestroy()
{
DestroyClient();
}
void Update()
{
if (_wrapper != null)
{
_wrapper.Update();
}
}
string foldertreeid = "6f631d00-131b-4474-a08d-83dbc6ee7fa6";
void OnGUI()
{
if (_wrapper == null)
{
GetBCClient();
}
if (!_wrapper.Client.IsAuthenticated())
{
GUILayout.Label("brainCloud Settings: " + _wrapper.Client.AppId);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Login"))
{
_debug = "AUTHENTICATING " + (_wrapper != null);
_wrapper.ResetStoredProfileId();
_wrapper.AuthenticateAnonymous(OnAuthSuccess, OnAuthFail);
}
GUILayout.EndHorizontal();
}
else
{
GUILayout.Label("Logged in : " + _wrapper.GetStoredProfileId());
GUILayout.Space(10);
if (!bCriticalState)
{
GUILayout.Label("Select Location: " + _path);
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("Select Folder or File");
if (GUILayout.Button("Select Folder"))
{
_path = EditorUtility.OpenFolderPanel("Select Folder to Upload", "", "");
if (_path.Length != 0)
{
var info = new DirectoryInfo(_path);
FileInfo[] fileInfo = info.GetFiles();
foreach (FileInfo file in fileInfo)
{
_pathToFilesToUpload.Add(file);
}
}
}
GUILayout.Space(10);
if (GUILayout.Button("Select File"))
{
_path = EditorUtility.OpenFilePanel("Select File to Upload", "", "");
if (_path.Length != 0)
{
FileInfo file = new FileInfo(_path);
_pathToFilesToUpload.Add(file);
}
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
// add input field of globaltreeid for transfering user files to the specified global folder
GUILayout.Label("Put your global folder id (aka: globalTreeId) here:", EditorStyles.boldLabel);
GUILayout.Space(5);
foldertreeid = EditorGUILayout.TextField("GlobalTreeId:", foldertreeid);
GUILayout.Space(10);
if (_pathToFilesToUpload.Count > 0)
{
if (GUILayout.Button("UPLOAD FILE" + (_pathToFilesToUpload.Count > 1 ? "S" : "")))
{
uploadNextFile();
}
}
}
// scroll view
GUILayout.Label("Selected Items");
scroll = GUILayout.BeginScrollView(scroll);
foreach (var r in _pathToFilesToUpload)
{
GUILayout.Label(r.Name);
}
GUILayout.EndScrollView();
}
GUILayout.Label("DEBUG " + _debug);
}
private void uploadNextFile()
{
bCriticalState = true;
if (_pathToFilesToUpload.Count > 0)
{
_wrapper.FileService.UploadFile("bulk_files",
_pathToFilesToUpload[0].Name, false, true,
_pathToFilesToUpload[0].ToString(), OnUploadSuccess, OnUploadFail);
}
else
{
_debug = "Transferring Files. Do not add more.";
_wrapper.ScriptService.RunScript("transferUserFilesToGlobalFiles", "{\"foldertreeid\":\"" + foldertreeid + "\"}", OnUploadScriptCompleteSuccess, OnUploadScriptCompleteFail);
}
}
private static void hierarchyWindowChanged()
{
if (_wrapper == null)
{
GetBCClient();
}
}
private static void DestroyClient()
{
if (_wrapper != null)
{
DestroyImmediate(_wrapper.gameObject);
_wrapper = null;
_path = "";
_pathToFilesToUpload.Clear();
}
}
private static void GetBCClient()
{
DestroyClient();
GameObject go = new GameObject();
go.name = "bcBulkUpload";
_wrapper = go.AddComponent<BrainCloudWrapper>();
_wrapper.WrapperName = "bcBulkUpload";
_wrapper.InitWithApps();
}
private void OnAuthSuccess(string in_json, object obj)
{
_debug = "AUTHENTICATED";
}
private void OnAuthFail(int status, int reasonCode, string jsonError, object cbObject)
{
_debug = "FAILED";
}
private void OnUploadScriptCompleteSuccess(string in_json, object obj)
{
bCriticalState = false;
_debug = "User Files Transfered";
}
private void OnUploadScriptCompleteFail(int status, int reasonCode, string jsonError, object cbObject)
{
bCriticalState = false;
_debug = "FAILED";
}
private void OnUploadSuccess(string in_json, object obj)
{
_pathToFilesToUpload.RemoveAt(0);
_debug = "Upload " + (_pathToFilesToUpload.Count > 0 ? "in progress" : "COMPLETE");
uploadNextFile();
}
private void OnUploadFail(int status, int reasonCode, string jsonError, object cbObject)
{
_debug = "Upload Failed FAILED";
}
}
}
Once your editor window reloads, you will see the
BCTools
menu is added to your Unity editor. ClickUpload Files To App
under this menu to open a pop up window.
Click
Login
to authenticate a user to your app, then clickSelect Folder
to select all files under one of your local storage folder, or clickSelect File
to choose file one by one.
Then click
UPLOAD FILES
button, all the selected files will get uploaded to the brainCloud user file system and then be transfered to the global file system. Note: If you want to store the selected files with a different folder in the brainCloud global tree structure, just replace the default id with that folder id inGlobalTreeId
field before hittingUPLOAD FILES
button.